Rails 4 - 通过 link_to 传递参数? [英] Rails 4 - Passing Params via link_to?
本文介绍了Rails 4 - 通过 link_to 传递参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表单 - 根据用户单击哪个链接来显示表单,我希望将不同的隐藏参数传递给记录并在提交时保存.有没有很好的方法来做到这一点?提前致谢!
I have a single form - depending which link the user clicks to display the form, I want different hidden params to be passed to the record and saved upon submission. Is there a nice way to do this? Thanks in advance!
例如:
<%= link_to 'General Request', new_request_path %>
<%= link_to 'Project Request', new_request_path %> ### -> set request.project = true
<%= link_to 'Administrative Request', new_request_path %> ### -> set request.admin = true
推荐答案
对于您的示例,您可以使用:
For your examples, you'd use:
<%= link_to 'Project Request', new_request_path(project: true) %>
这会产生一个像 http://127.0.0.1:3000/request?project=true
和
<%= link_to 'Administrative Request', new_request_path(admin: true) %>
这会产生一个像 http://127.0.0.1:3000/request?admin=true
这篇关于Rails 4 - 通过 link_to 传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文