1

我希望能够在提交时添加一个额外的查询字符串参数,该参数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某种方式添加吗?

谢谢!

4

2 回答 2

1

由于您首先将其发送到您的控制器,因此您可以在发送之前操作控制器方法中的参数:

params[:classrooms_query] = params[:classrooms_search_textbox]

然后继续使用这些参数发送到其他服务。无需添加隐藏字段标签或使用一些花哨的 JS 代码。

于 2012-08-10T21:29:20.843 回答
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());
});

那将实现您想要的。尽管如此,也许重构控制器或视图以使其不存在这种冲突符合您的利益。

于 2012-08-10T21:29:05.623 回答