user681768
user681768

Reputation:

Why does '/' match correctly to a Rack application in my Rails 3 app, but no other route does?

I'm using Rails 3.0.6.

In my lib directory I have a example_app.rb, which is simply a Sinatra app:

class ExampleApp < Sinatra::Base
  get '/' do
    "Hello from Sinatra"
  end
end

And I'm autoloading it with the application.rb config.auto_load_paths configuration.

In my routes file, I have only:

match "/" => ExampleApp

And that matches fine when I run the rails server (Webrick). However if I try:

match "/example" => ExampleApp

Visiting localhost:3000/example gives me a 'No route matches "/example"' error. Running rake routes shows the route though:

example  /example(.:format) {:to=>ExampleApp}

If I try and match '/example' against a controller action it works fine, but not to that Sinatra app up above, so I'm not sure what's going on. I know there's something small I'm missing that I'm not finding in the routing documentation on the Rails site.

Thanks for any help.

Upvotes: 2

Views: 261

Answers (1)

Dogbert
Dogbert

Reputation: 222108

The problem is that your Sinatra app only responds to requests made to /. You need to either add

get '/example'

or do a wildcard match using *

get '*' do
  "Hello from Sinatra"
end

Upvotes: 3

Related Questions