Skip to content

Loading…

Sinatra::Namespace doesn't seem to work #47

Closed
evanleck opened this Issue · 6 comments

4 participants

@evanleck

First off, I'm sorry I can't give you more context or (even better) provide a pull request. I haven't had any luck getting any more detail or information about what's going on and understanding the source for the namespace module is a little beyond me (I've tried :/).

That said, I can't seem to get namespace to work at all outside of a contrived, single Ruby file. I have a main controller file that does a require of another file with this:

# Gemfile
gem 'sinatra'
gem 'sinatra-flash', require: 'sinatra/flash'
gem 'sinatra-contrib', require: 'sinatra/contrib'

# controller.rb
Dir.glob('./**/*.rb') do |file|
  require file.gsub(/\.rb/, '')
end

get '/' do
  erb 'pages/home'
end

# sessions.rb
namespace '/login' do
  get do
    "oh hey"
  end
end

When I go to '/login' I get a "Sinatra doesn't know that ditty" 404. I'm on Sinatra 1.3.2, Sinatra-contrib 1.3.1 on Ruby 1.9.3-p125.

@evanleck

If anyone else is having this issue I've sorta janky-fixed this like so:

'/login'.tap do |path|
  get path do
    "haii"
  end

  post path do
    "oooh nice post"
  end
end

It works and cleans things up a little bit... sort of... I guess :/

@mattreduce

@l3ck the only way I could get namespace to work in a modular app with your structure is if my sessions.rb is written this way:

class App < Sinatra::Base
  register Sinatra::Namespace

  namespace '/login' do
    get do
      "oh hey"
    end
  end
end

I'm registering the Sinatra::Namespace extension again in an individual controller file.

@SaitoWu

did u add rake to ur gemfile?

if so, sinatra-contrib namespace will conflict with rake's dsl namespace in the classic mode sinatra app.

rake's namespace is defined here: https://github.com/jimweirich/rake/blob/master/lib/rake/dsl_definition.rb#L96

to fix this problem:

  • remove rake from ur gemfile

or

  • mod ur sinatra classic mode app to modular mode.
@evanleck

You're a friggin' genius @SaitoWu ! That did it (I'm on a classic style app)!

@locks

@l3ck I think you could probably close this, doesn't seem like it's a Sinatra::Namespace bug per se.
This is nice info though, I was checking Issues because I just started using this extension.

@evanleck

Yeah good point, closing it.

@evanleck evanleck closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.