Ruby-on-Rails快速搜索表单

时间:2013-09-24 10:02:11

标签: ruby-on-rails ruby-on-rails-4

我有以下搜索表单:

<%= form_tag edit_lot_path(:id), :class => "form-search", method: :get do %>
  <div class="input-append">
    <%= text_field_tag :id, nil, :placeholder => "Lot #", :class => "input-mini search-query" %>
    <%= button_tag(type: "submit", class: "btn") do %>
       <i class="icon-search"></i>
    <% end %>
  </div>
<% end %>

我要做的是传递搜索字段(id到路径),以便用户可以快速查找id。不幸的是我收到错误Couldn't find Lot with id=id

请注意这是使用rails 4。

1 个答案:

答案 0 :(得分:1)

对于搜索实施,理想情况下您应该有单独的操作,例如&#39;搜索&#39;在你的手中。

路线

resources :lots do
  get :search, :on => :collection
end

在视图文件中

<%= form_tag search_lots_path, :class => "form-search", method: :get do %>
  <div class="input-append">
    <%= text_field_tag :id, nil, :placeholder => "Lot #", :class => "input-mini search-query" %>
    <%= button_tag(type: "submit", class: "btn") do %>
       <i class="icon-search"></i>
    <% end %>
  </div>
<% end %>

在批次控制器中

def search
 my_search_string = params['id']
 // YOUR CODE GOES HERE
end