Rails - 在 link_to 中传递参数 [英] Rails - passing parameters in link_to

查看:34
本文介绍了Rails - 在 link_to 中传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的账户索引页面列出了所有账户,每个账户都有一个+服务"链接;这会将用户引导至/my_services/new 页面,并根据在帐户索引"页面上单击的链接,使用适当的 ID 预先填充 account_id 字段.

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.

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

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.

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

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.

编辑

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

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. My routes.rb for this part now looks like this:

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 } ) %>

推荐答案

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

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

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

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.

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

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

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:

@my_service.account_id = current_user.account_id 

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

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.

希望能帮到你

这篇关于Rails - 在 link_to 中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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