开发者

Routing to match a singular name instead of the plural generated by ':resources'

开发者 https://www.devze.com 2023-02-04 09:35 出处:网络
In the \'config/routes.rb\' of my RoR3 application I have this code resources :users so that I can go to http://application.local/users/2.

In the 'config/routes.rb' of my RoR3 application I have this code

resources :users

so that I can go to http://application.local/users/2.

Anyway I would like to match for it http://a开发者_运维技巧pplication.local/user/2, but I don't know how to change the 'routes.rb' to do that.

Can somebody help me?


Rails does have singular resources but they're for routes which there is only one record/id which is being referenced (for example /profile to show the logged in user's profile).

If you're needing a normal resource (which can accept IDs), then you can use :path to change how the route looks:

resources :users, :path => "user"

Will give you these routes:

    users GET    /user(.:format)                    {:controller=>"users", :action=>"index"}
          POST   /user(.:format)                    {:controller=>"users", :action=>"create"}
 new_user GET    /user/new(.:format)                {:controller=>"users", :action=>"new"}
edit_user GET    /user/:id/edit(.:format)           {:controller=>"users", :action=>"edit"}
     user GET    /user/:id(.:format)                {:controller=>"users", :action=>"show"}
          PUT    /user/:id(.:format)                {:controller=>"users", :action=>"update"}
          DELETE /user/:id(.:format)                {:controller=>"users", :action=>"destroy"}
0

精彩评论

暂无评论...
验证码 换一张
取 消