Ruby-on-rails Rails - 在 link_to 中传递参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10773695/
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-03 03:27:27  来源:igfitidea点击:

Rails - passing parameters in link_to

ruby-on-rails

提问by bdx

My Accounts index page lists all accounts, and per account has a link to "+ Service"; this should direct the user to the /my_services/new page and have pre-populated the account_id field with the appropriate ID, depending on which link was clicked on the Accounts index page.

我的账户索引页面列出了所有账户,每个账户都有一个“+服务”链接;这应该将用户引导至 /my_services/new 页面,并使用适当的 ID 预先填充 account_id 字段,具体取决于在 Accounts index 页面上单击的链接。

I have debug(params) at the bottom of every page, and with what I'm trying, I'm not getting anything other than :controller and :action showing up in my parameters for the /my_services/new page.

我在每个页面的底部都有 debug(params) ,并且通过我正在尝试的内容,除了 :controller 和 :action 显示在我的 /my_services/new 页面的参数中之外,我没有得到任何其他信息。

The link I've been trying is this:

我一直在尝试的链接是这样的:

link_to "+ Service", "my_services/new", :account_id => acct.id

I then also have logic in the services controller:

然后我在服务控制器中也有逻辑:

def new
  @my_service = MyService.new
  if params[:account_id]
    @my_service.account_id = params[:account_id]
  end
end

Can someone help with the proper way of doing this? I haven't yet been able to get it going with with a few nasty little hacky things I tried.

有人可以帮助正确地做到这一点吗?我还没有能够用我尝试过的一些讨厌的小东西来让它继续下去。

EDIT

编辑

Turns out, if someone is looking at this answer in future, nested resources (possibly with the shallow: trueoption in routes.rb) seem to be the way to go. My routes.rb for this part now looks like this:

事实证明,如果将来有人在看这个答案,嵌套资源(可能带有shallow: trueroutes.rb 中的选项)似乎是要走的路。我这部分的 routes.rb 现在看起来像这样:

resources :accounts, shallow: true do
  resources :services
end

My links now look like this:

我的链接现在看起来像这样:

<%= link_to "+ Service", new_service_path(:service => { :account_id => @account.id } ) %>

回答by Viktor Trón

First of all, link_to is a html tag helper, its second argument is the url, followed by html_options. What you would like is to pass account_id as a url parameter to the path. If you have set up named routes correctly in routes.rb, you can use path helpers.

首先,link_to 是一个 html 标签助手,它的第二个参数是 url,其次是 html_options。您想要的是将 account_id 作为 url 参数传递给路径。如果您在 routes.rb 中正确设置了命名路由,则可以使用路径助手。

link_to "+ Service", new_my_service_path(:account_id => acct.id)

I think the best practice is to pass model values as a param nested within :

我认为最好的做法是将模型值作为嵌套在 param 中传递:

link_to "+ Service", new_my_service_path(:my_service => { :account_id => acct.id })

# my_services_controller.rb
def new
  @my_service = MyService.new(params[:my_service])
end

And you need to control that account_id is allowed for 'mass assignment'. In rails 3 you can use powerful controls to filter valid params within the controller where it belongs. I highly recommend.

并且您需要控制允许“批量分配”的 account_id。在 rails 3 中,您可以使用强大的控件来过滤其所属控制器内的有效参数。我强烈推荐。

http://apidock.com/rails/ActiveModel/MassAssignmentSecurity/ClassMethods

http://apidock.com/rails/ActiveModel/MassAssignmentSecurity/ClassMethods

Also note that if account_id is not freely set by the user (e.g., a user can only submit a service for the own single account_id, then it is better practice not to send it via the request, but set it within the controller by adding something like:

还要注意,如果 account_id 不是用户自由设置的(例如,用户只能为自己的单个 account_id 提交服务,那么最好不要通过请求发送它,而是通过添加一些东西在控制器中设置它喜欢:

@my_service.account_id = current_user.account_id 

You can surely combine the two if you only allow users to create service on their own account, but allow admin to create anyone's by using roles in attr_accessible.

如果您只允许用户在他们自己的帐户上创建服务,但允许管理员使用 attr_accessible 中的角色创建任何人的服务,那么您肯定可以将两者结合起来。

hope this helps

希望这可以帮助

回答by abhas

Try this

尝试这个

link_to "+ Service", my_services_new_path(:account_id => acct.id)

it will pass the account_id as you want.

它会根据需要传递 account_id 。

For more details on link_to use this http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

有关 link_to 的更多详细信息,请使用此http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

回答by Vish

link_to "+ Service", controller_action_path(:account_id => acct.id)

If it is still not working check the path:

如果仍然无法正常工作,请检查路径:

$ rake routes