Ruby-on-rails 更改 Rails 路由中的 id 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2837102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:40:35  来源:igfitidea点击:

Changing the id parameter in Rails routing

ruby-on-railsroutingruby-on-rails-3

提问by joeellis

Using Ruby on Rails 3's new routing system, is it possible to change the default :id parameter

使用 Ruby on Rails 3 的新路由系统,是否可以更改默认的 :id 参数

resources :users, :key => :username

come out with the following routes

出来的路线如下

/users/new
/users/:username
/users/:username/edit
...etc

I'm asking because although the above example is simple, it would be really helpful to do in a current project I'm working on.

我问是因为虽然上面的例子很简单,但在我正在从事的当前项目中做真的很有帮助。

Is it possible to change this parameter, and if not, is there a particular reason as to why not?

是否可以更改此参数,如果不能,是否有特殊原因?

采纳答案by Ju Nogueira

If I understand you correctly, what you want is to have the usernameinstead of idin your url, right?

如果我理解正确,您想要的是在您的网址中使用username而不是id,对吗?

You can do that by overriding the to_parammethod in your model. You can get more information here.

您可以通过覆盖to_param模型中的方法来做到这一点。您可以在此处获取更多信息。

回答by Ujjwal Thaakar

In your route you can use

在您的路线中,您可以使用

resources :user, param: :username

回答by Matt

For Ruby on Rails 4.1.4 (and possibly earlier versions) you need to do what bothj..and Ujjwalsuggested:

对于Ruby on Rails的4.1.4(甚至更早的版本),你需要做什么Ĵ..Ujjwal建议:

1) In config/routes.rb, add:

1) 在 中config/routes.rb,添加:

resources :user, param: :username

2) In app/models/user.rb, add:

2) 在 中app/models/user.rb,添加:

def to_param
  username
end

In you only do #1 then all your routes will be correct, as can be seen from rake routes:

如果你只做 #1,那么你的所有路线都是正确的,从rake routes以下可以看出:

$ rake routes
    Prefix Verb   URI Pattern                    Controller#Action
user_index GET    /user(.:format)                user#index
           POST   /user(.:format)                user#create
  new_user GET    /user/new(.:format)            user#new
 edit_user GET    /user/:username/edit(.:format) user#edit
      user GET    /user/:username(.:format)      user#show
           PATCH  /user/:username(.:format)      user#update
           PUT    /user/:username(.:format)      user#update
           DELETE /user/:username(.:format)      user#destroy

However the helper methods that construct a url based on a Userinstance will still include the idin the url, e.g. /user/1. To get the usernamein the constructed urls, you need to override to_paramas in #2.

然而,基于User实例构造 url 的辅助方法仍将包含id在 url 中,例如/user/1. 要username在构造的 url 中获取 ,您需要to_param像 #2 一样覆盖。

回答by Trav McKinney

The FriendlyIdgem can help you out with this. Ryan Bates has a good video tutorialon using this gem.

FriendlyId宝石可以帮你出这一点。Ryan Bates 有一个关于使用这个 gem的很好的视频教程

回答by KULKING

Although the answer has been accepted by the asker, but there is a simple approach to do this. Write this code in your controller.

虽然答案已被提问者接受,但有一个简单的方法可以做到这一点。在您的控制器中编写此代码。

authorize_resource :find_by => :username

and in your view, where you want to show the link, write this code.

并在您想要显示链接的位置编写此代码。

<%= link_to "Username", user_path(u.username) %>

You don't need any other changes in your routes or controller.

您不需要对路由或控制器进行任何其他更改。

UPDATE:This will only work if you are using CanCan gem.

更新:这仅在您使用 CanCan gem 时才有效。

回答by Harish Shetty

Pass the user name as the input to the path helpers.

将用户名作为输入传递给路径助手。

In your view code:

在您的视图代码中:

user_path(u.username) #/users/john

In your controller treat the idreceived as username:

在您的控制器中,将id收到的视为username

def show
  user = User.find_by_username!(params[:id])
  ...
end