[英]How do I submit my form to the correct url using a select box? [Rails 3]
我希望我的表单提交以遵循此模式的网址
/tournaments/:id/matches
在我的应用中,我可以成功建立与此行相关的href
a.btn.btn-success href=url_for(controller: :tournaments, action: :matches, id: tournament.id) Button text
这是我的表格(使用苗条)
= form_tag({:controller => :tournaments, :action => :matches}, :method => :get) do
select name="id"
- @all_tournaments.each do |tournament|
- if tournament.id == @tournament.id
option value="#{tournament.id}" selected="selected" #{tournament.name}
- else
option value="#{tournament.id}" #{tournament.name}
br
input.btn.btn-success type='submit' value='Submit'
表单代码正确地构造了一个带有选择框的表单。 但是当我进入这个网址时:
/tournaments/4/matches
并提交选择了选项6的表格,它将我发送到此网址
/tournaments/4/matches?utf8=%E2%9C%93&id=6
id = 6在查询部分中。 我想要的是将表单提交至:
/tournaments/6/matches
我该如何构造我的表格才能正常工作?
我正在运行ruby 1.9.3和rails 3.2.11。
我的路线
resources :tournaments do
get 'matches', :on => :member
如果您必须将网址指定为/tournaments/:id/matches
,则我认为这是javascript的工作,而不是rails
您可以在选择锦标赛时触发事件并修改表格的网址。
$(function(){
$('select#id').change(function(){
$('form').attr('url', '/tournaments/'+$(this).val()+'/matches');
});
});
看小提琴
或者,您可以定义诸如get 'tournaments/matches'
类的路线。 只需将id放入参数列表即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.