ruby-on-railsrubyformatnamed-routing

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!


Solution

  • 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"}