简体   繁体   English

Rails-在link_to中传递参数

[英]Rails - passing parameters in link_to

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页面,并根据在Accounts索引页面上单击的链接,用适当的ID预先填充account_id字段。

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: true option in routes.rb) seem to be the way to go. 事实证明,如果将来有人在看这个答案,嵌套资源(可能在routes.rb中使用shallow: true选项)似乎是解决之道。 My routes.rb for this part now looks like this: 我这部分的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 } ) %>

First of all, link_to is a html tag helper, its second argument is the url, followed by html_options. 首先,link_to是html标签帮助器,其第二个参数是url,后跟html_options。 What you would like is to pass account_id as a url parameter to the path. 您想要将account_id作为URL参数传递到路径。 If you have set up named routes correctly in routes.rb, you can use path helpers. 如果您在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 : 我认为最佳实践是将模型值作为嵌套在内部的参数传递:

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'. 并且您需要控制允许“ mass assignment”使用account_id。 In rails 3 you can use powerful controls to filter valid params within the controller where it belongs. 在rails 3中,您可以使用强大的控件来过滤控制器所属的控制器中的有效参数。 I highly recommend. 我强烈推荐。

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 (eg, 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 希望这可以帮助

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

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

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

$ rake routes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM