How to rename the default identifier param “id” in Rails' map.resources()?

后端 未结 2 2083
谎友^
谎友^ 2021-02-02 00:53

I like all the default routes that are generated by Rail\'s map.resources. But, there are cases where I would like to use a non-numeric identifier in my routes.

2条回答
  •  你的背包
    2021-02-02 01:26

    As of Rails 2.3, it's not possible to change the parameter name and still use the automatic routing that #resources provides.

    As a workaround, you can map articles with a :path_prefix and :name_prefix:

    map.resources :articles, :path_prefix => "/users/:login",
                             :name_prefix => "user_"
    

    The :path_prefix affects the URL, and the :name_prefix affects the generated named routes, so you'll end up with these routes:

        user_articles GET    /users/:login/articles(.:format)          {:controller=>"articles", :action=>"index"}
                      POST   /users/:login/articles(.:format)          {:controller=>"articles", :action=>"create"}
     new_user_article GET    /users/:login/articles/new(.:format)      {:controller=>"articles", :action=>"new"}
    edit_user_article GET    /users/:login/articles/:id/edit(.:format) {:controller=>"articles", :action=>"edit"}
         user_article GET    /users/:login/articles/:id(.:format)      {:controller=>"articles", :action=>"show"}
                      PUT    /users/:login/articles/:id(.:format)      {:controller=>"articles", :action=>"update"}
                      DELETE /users/:login/articles/:id(.:format)      {:controller=>"articles", :action=>"destroy"}
    

    As a general rule-of-thumb, though, I'd stick with the Rails default convention of :user_id, with the routing you posted in your question. It's generally understood that :id and :user_id don't necessarily imply "numeric identifier" — they imply "resource identifier," whatever that might be. And by sticking to the default convention, your code will be easier to understand for anyone who's used resource routes in Rails.

    To use a non-numeric identifier for a resource, just redefine #to_param in your model. Then, make sure to use a finder in your controller that will find by this identifier (rather than the numeric ID), such as User#find_by_login!.

提交回复
热议问题