Alfred
Alfred

Reputation: 7091

How to add :format options to a named route in Rails?

I've got a named route called profile and I would like to be able to access it as json. But when I look at my rake routes output I see that the (.:format) is missing. How do I add it to a named route?

user GET    /users/:id(.:format)    {:action=>"show", :controller=>"users"}
profile     /:username              {:action=>"show", :controller=>"users"}

Thanks in advance!

Upvotes: 2

Views: 2507

Answers (1)

Thorbjørn Hermansen
Thorbjørn Hermansen

Reputation: 3552

Add .format in your path specification, like this:

map.profile '/:profile.:format', :controller => 'users', :action => 'show'

Then you'll end up with:

profile    /:profile(.:format)     {:action=>"show", :controller=>"users"}

Upvotes: 7

Related Questions