搜索不在部分工作

时间:2014-05-21 18:14:42

标签: ruby-on-rails search partials

我在2个不同的视图中有2个搜索字段。他们都搜索机构,除了造型之外都是相同的。

一个在welcome / index.html.erb中,效果很好。

另一个是部分名为_nav.html.erb,从application.html.erb调用,根本不起作用。

我需要将一些内容传递给_nav部分,以便正确处理搜索表单吗?

欢迎/ index.html.erb

<div>
  <%= form_tag(establishments_path, method: "get") do %>
    <div class="form-group">
      <%= text_field_tag :search, params[:search], 
          placeholder: "Enter the name of an eatery", class: "form-control" %>
      <div class="actions"><%= submit_tag "Search", :name => nil, class: "btn btn-primary" %></div>
    </div>
 <% end %>
</div>

_nav.html.erb

<form class="navbar-form navbar-left" role="search">
  <%= form_tag(establishments_path, method: "get") do %>
    <div class="form-group">
      <%= text_field_tag :search, params[:search], 
          placeholder: "Search", class: "form-control" %></div>
      <%= submit_tag "Search", :name => nil, class: "btn btn-primary" %>
  <% end %>
</form>

application.html.erb

<%= render 'layouts/nav' %>

EstablishmentsController /索引

def index
  if params[:search]
   @establishments = Establishment.search(params[:search].downcase).order("created_at ASC")
  else
    @establishments = Establishment.all.limit(25)
  end
end

1 个答案:

答案 0 :(得分:3)

您的form_tag已经包含在表单中:

<form class="navbar-form navbar-left" role="search">
#^^^^ Here
  <%= form_tag(establishments_path, method: "get") do %>
     #^^^^ and here again
    <div class="form-group">
      <%= text_field_tag :search, params[:search], 
          placeholder: "Search", class: "form-control" %></div>
      <%= submit_tag "Search", :name => nil, class: "btn btn-primary" %>
  <% end %>
</form>

当您单击提交标记时,它会提交错误的表单(第一个)。

您应该像这样使用它:

<%= form_tag(establishments_path, method: :get, class: 'navbar-form navbar-left', role: 'search') do %>
  <div class="form-group">
    <%= text_field_tag :search, params[:search], placeholder: "Search", class: "form-control" %></div>
    <%= submit_tag "Search", :name => nil, class: "btn btn-primary" %>
<% end %>