如何重命名默认标识符参数"id".在Rails的map.resources()中? [英] How to rename the default identifier param "id" in Rails' map.resources()?

查看:113
本文介绍了如何重命名默认标识符参数"id".在Rails的map.resources()中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢Rail的 map.resources 生成的所有默认路线.但是,在某些情况下,我想在路线中使用非数字标识符.例如,如果嵌套路线包含用户及其文章,则标准路线可以这样写:

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. For example, If have a nested route consist of users and their articles, a standard route could be written as such:

map.resources :users, :has_many => [:articles] # => e.g. '/users/:id/articles/:id'

但是,有许多优点/理由不使用Rails生成的默认数字标识符.有没有一种方法可以将默认的:id参数替换为我选择的另一个规范标识符,而不会导致为每个标准操作编写自定义路由?假设我要采用以下格式的路线:

However, there are many advantages / reasons not to use the default numerical identifier generated by Rails. Is there a way to replace the default :id params to another canonical identifier of my choice without resulting to writing custom routes for every standard action? Say if I want a route in the following format:

'/users/:login/articles/:id'

使用 map.resources 是否可以实现这种路线?

Is this kind of routes achievable using map.resources?

推荐答案

从Rails 2.3开始,无法更改参数名称,而仍使用#resources提供的自动路由.

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

作为一种解决方法,可以将articles:path_prefix:name_prefix映射:

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

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

:path_prefix影响URL,而:name_prefix影响生成的命名路由,因此您将得到以下路由:

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

不过,作为一般的经验法则,我会坚持使用Rails的默认约定:user_id以及您在问题中发布的路由.通常理解,:id:user_id不一定暗含数字标识符",它们暗含资源标识符",无论可能是什么.通过遵循默认约定,对于在Rails中使用资源路由的任何人,您的代码都将更易于理解.

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_param.然后,确保在控制器中使用一个可以通过此标识符(而不是数字ID)进行查找的查找器,例如User#find_by_login!.

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!.

这篇关于如何重命名默认标识符参数"id".在Rails的map.resources()中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆