Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Singular Resource mapping to plural controller in rails 3

#5820

resources :users do

  resource :wall do
    post :add_comment
  end
end

end

and the route generated is

user_wall POST /users/:user_id/wall(.:format) {:controller=>"walls", :action=>"create"}

new_user_wall GET /users/:user_id/wall/new(.:format) {:controller=>"walls", :action=>"new"}

edit_user_wall GET /users/:user_id/wall/edit(.:format) {:controller=>"walls", :action=>"edit"}

user_wall GET /users/:user_id/wall(.:format) {:controller=>"walls", :action=>"show"}

user_wall PUT /users/:user_id/wall(.:format) {:controller=>"walls", :action=>"update"}

user_wall DELETE /users/:user_id/wall(.:format) {:controller=>"walls", :action=>"destroy"}

Why the Singular Resource maps to plural controller "walls" not the singular form "wall"
Is this a bug ?

Reported by inetufo · October 16th, 2010 @ 08:26 AM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • State changed from new to invalid
    • Importance changed from to Low

    No, that's not a bug. Controllers are always plural, singular routes are just a way to route to without having to specify an ID. Check out the Routing guide:

    Because you might want to use the same controller for a singular route (/account) and a plural route (/accounts/45), singular resources map to plural controllers.

    Marking this as invalid, thanks for letting us know though. :)

    October 16th, 2010 @ 08:40 AM

  2. inetufo
    inetufo

    Thanks for the reply, but in my situation, a user just has one wall.I mean the url is like http://xxx/user/:user_id/wall, and will never be http://xxx/user/:user_id/wall/:id, the "wall" contoller is just used to show something just like comments to a user, and will never have an id param.Howerver, i changed the controller name to WallsController, and it works, but it looks a little weird...Any other way to write this?

    October 16th, 2010 @ 09:17 AM

  3. inetufo
    inetufo

    I found a solution already by specifying the :controller param

    resources :users do

    resource :wall, :controller => "wall" do

    post :add_comment
    

    end end

    October 17th, 2010 @ 04:12 PM