更改 Rails 路由资源中 :id 参数的名称 [英] Change the name of the :id parameter in Routing resources for Rails

查看:42
本文介绍了更改 Rails 路由资源中 :id 参数的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,了解如何更改动态参数槽,并发现这篇文章完全正确.该帖子是 https://thoughtbot.com/blog/rails-patch-change-the-name-of-the-id-parameter-in

基本上它的作用是,如果以下是路线:

map.resources :clients, :key =>:client_name 做 |客户端|client.resources :sites, :key =>:name 做 |站点|site.resources :articles, :key =>:标题结尾结尾

这些路由创建以下路径:

/clients/:client_name/clients/:client_name/sites/:name/clients/:client_name/sites/:site_name/articles/:title

一种解决方案是覆盖模型中的 def to_param 方法,但我希望在不触及模型本身的情况下这样做.

但是由于它是针对 Rails 2.x,我如何才能为 Rails 3 实现相同的目标?

更新

此应用正在使用 Mongoid.不是 AR.所以,gem 友好不能使用 afaik.

解决方案

Rails 4 &5

在 Rails 4 中,添加了 :param 选项,这似乎完全符合您的要求.你可以看看Rails 3 代码Rails 4 代码相比.>

详情

您可以在 routes.rb 文件中轻松实现:

# config/routes.rb资源:帖子,参数::slug# app/controllers/posts_controller.rb# ...@post = Post.find_by(slug: params[:slug])# ...

从 Rails 4 发布开始,此功能记录在 Rails 指南中.

轨道 3

不幸的是,在 Rails 3 中,resources:key 选项被删除了,所以你不能再简单地通过传递以这种方式创建的路由的名称在一个额外的选项.

详情

我假设您在过去的一年中已经以某种方式让应用程序以您想要的方式工作,但是我将在 routes.rb 中寻找一种方法来获得您在 Rails 3 中描述的效果.它只涉及比 to_param 方法更多的工作.您仍然可以在使用 scopematch(或者它的堂兄弟 getput、<代码>发布和<代码>删除).您只需在匹配器中输入您想要的参数名称:

get 'clients/:client_name', :to =>'clients#show', :as =>客户范围 'clients/:client_name' 做获取 'sites/:name', :to =>'sites#show', :as =>地点结尾

您必须手动添加 resources 自动为您创建的所有路由,但它会实现您的需求.您还可以有效地将 :controller 选项与 scope 和额外的 scope 块一起使用,以消除一些重复.

<小时>

编辑(2014 年 5 月 8 日): 让答案更明显地包含 Rails 3 和 Rails 3 的信息.4. 更新代码链接以转到确切的行号和提交,以便它们可以工作更长时间.

编辑(2014 年 11 月 16 日):Rails 4 现在应该位于顶部并包含相关信息,因为它是 Rails 的当前版本已经有一段时间了.

编辑(2016 年 8 月 9 日):反映该解决方案在 Rails 5 中仍然有效,并更新过时的链接.

I looked around on how to change the dynamic params slot and found this post that does the exact thing. The post is https://thoughtbot.com/blog/rails-patch-change-the-name-of-the-id-parameter-in

Basically what it does is, if following is the routes:

map.resources :clients, :key => :client_name do |client|
  client.resources :sites, :key => :name do |site|
    site.resources :articles, :key => :title
  end
end

These routes create the following paths:

/clients/:client_name
/clients/:client_name/sites/:name
/clients/:client_name/sites/:site_name/articles/:title

One solution is to override the def to_param method in the model, but I want this without touching the model itself.

But since its for Rails 2.x, how can I achieve the same for Rails 3?

Update

This app is using Mongoid. Not AR. So, the gem friendly cannot be used afaik.

解决方案

Rails 4 & 5

In Rails 4, the :param option was added, which seems to do exactly what you're looking for. You can take a look at the Rails 3 code compared to the Rails 4 code.

Details

You can easily implement this in your routes.rb file:

# config/routes.rb

resources :posts, param: :slug

# app/controllers/posts_controller.rb

# ...
@post = Post.find_by(slug: params[:slug])
# ...

As of the release of Rails 4, this functionality is documented in the Rails Guides.

Rails 3

Unfortunately, in Rails 3, the :key option for resources was removed, so you can no longer easily change the name for routes created in this way by just passing in an extra option.

Details

I assume you've already somehow gotten the application working the way you want in the past year, but I will go into a way to get the effect you describe in Rails 3 in routes.rb. It will just involve a bit more work than the to_param method. You can still define custom parameters in routes defined using scope and match (or it's cousins get, put, post, and delete). You simply write in the parameter name you want in the matcher:

get 'clients/:client_name', :to => 'clients#show', :as => client

scope 'clients/:client_name' do
  get 'sites/:name', :to => 'sites#show', :as => site
end

You would have to manually add all the routes that resources automatically creates for you, but it would achieve what you're looking for. You could also effectively use the :controller option with scope and additional scope blocks to take out some of the repetition.


EDIT (May 8, 2014): Make it more obvious the answer contains information for both Rails 3 & 4. Update the links to the code to go to exact line numbers and commits so that they should work for a longer period of time.

EDIT (Nov 16, 2014): Rails 4 should be at the top now and include relevant information as it's been the current version of Rails for quite some time now.

EDIT (Aug 9, 2016): Reflect that the solution still works in Rails 5, and update outdated links.

这篇关于更改 Rails 路由资源中 :id 参数的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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