Rails 4 – 通过link_to传递参数?
我有一个表单 – 根据用户点击显示表单的链接,我希望将不同的隐藏参数传递给记录并在提交时保存。 有一个很好的方法来做到这一点? 提前致谢!
例如:
### -> set request.project = true ### -> set request.admin = true
对于您的示例,您将使用:
<%= 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
这样的链接
我认为有两种方法可以完成你想要做的事情。
-
创建3个不同的路由来完成不同类型的请求。 例如,
new_request_path
,new_project_request_path
,new_admin_request_path
。 -
如果您要请求新项目,请使用
<%= link_to 'Project Request', new_request_path(:request_type => 'project') %>
。 在控制器中,您可以处理不同的请求类型。
def new case params[:request_type] when 'general' do_something when 'project' do_something_1 when 'admin' do_something_else end ... end