Rails Routing with Namespace and format (for api)
I know this isn't RESTful, but for now, I'm trying to set up an api/v1 controller. Ideally I would like to call it like this:
site.com/api/v1/verify.xml
But right now I c开发者_StackOverflow社区an't get the .xml to work. I have the following route so far:
map.namespace :api do |api|
api.connect ':controller/:action/:id'
end
I can do /api/v1/verify
but I don't think it is using the route above. For some reason it is still hitting my catch all route also even though it displays the right page.
map.connect '*path', :controller => 'application', :action => 'redirect_main'
So:
1) how do I get the.format
on there?
2) And how do I make it not hit my catch all route?1) how do I get the .format on there?
api.connect ':controller/:action/:id.:format'
2) And how do I make it not hit my catch all route?
I believe your catch-all route should be the last one on routes.rb
. This way it should work...
Wouldn't you just need to add .:format
?
map.namespace :api do |api|
api.connect ':controller/:action/:id.:format'
end
If you aren't using named routes or resources, you have to specify every combination of path you want to handle, including the file extension.
Adding api.connect ':controller/:action/:id.:format'
back to your api
namespace will give you access to params[:format]
to respond to.
精彩评论