我希望能够在提交时添加一个额外的查询字符串参数,该参数与用户将键入的classrooms_search_textbox
的值相同。我该怎么做?
<%= form_tag classrooms_path, :method => :get, :id => "classrooms_search_form" do %>
<%= text_field_tag "classrooms_search_textbox", "Find a classroom" %>
<%= submit_tag "Find", :id => "classrooms_search_button", :class => "button" %>
我是否需要添加hidden_tag
(如果是,我将如何进行此操作?)或者我可以以某种方式添加到classrooms_path
吗?
谢谢!
答案 0 :(得分:1)
由于您首先将控制器发送给控制器,因此您可以在发送控制器之前操作控制器方法中的参数:
params[:classrooms_query] = params[:classrooms_search_textbox]
然后继续使用这些参数发送给其他服务。没有必要添加隐藏的字段标签或使用一些花哨的JS代码。
答案 1 :(得分:0)
<%= form_tag classrooms_path, :method => :get, :id => "classrooms_search_form" do %>
<%= text_field_tag "classrooms_search_textbox", "Find a classroom" %>
<%= hidden_field_tag "classrooms_query" %>
<%= submit_tag "Find", :id => "classrooms_search_button", :class => "button" %>
$('#classrooms_search_form').submit(function() {
$('#classrooms_query').value($(classrooms_search_textbox.value());
});
那将实现你想要的。尽管如此,重构控制器或视图可能符合您的利益,这样它就不会发生这种冲突。