Rails - 简单的搜索功能

时间:2016-01-07 12:29:05

标签: ruby-on-rails

我对rails非常陌生,正在建立一个网上商店只是为了写一些铁轨。我现在正在实现一个简单的搜索功能,并得到一些我无法解释的奇怪行为。

模型方法:

def index
  if params[:search]
    @products = Product.search(params[:search])
  else
    @products = []
    #Only lists available products (in cart counts as available)
    @available_items = Item.where(user_id: nil).select(:product_id).uniq
    @available_items.each do |item|
      @products << item.product
    end
  end
end

控制器方法:

<%= form_tag(products_path, :method => "get", id: "search-form", enforce_utf8: false) do %>
  <%= text_field :search, params[:search], placeholder: "Search..." %>
  <%= submit_tag "Search", :name => nil %>
<% end %>

搜索表单:

1. Map (contains Image and two objects of Location class)
2. GPSPoint (contains two objects of ICoordinate)
3. ImagePoint (contains two int variables)
4. Location (contains GPSPoint and ImagePoint)

当我触发搜索时,我没有得到任何结果,网址看起来像这样:

  

here

当我删除&#39;%5B%5D&#39;从网址上它一切正常,我得到了我的结果。 &#39;%5B%5D&#39;代表&#39; []&#39;在URI编码中,无法弄清楚它来自何处。任何帮助将不胜感激!

最佳, 保罗

1 个答案:

答案 0 :(得分:2)

reference text_field text_field_tag <%= text_field :search, params[:search], placeholder: "Search..." %> 帮助{1}}:

name=search[]

它会为您提供search[]='text'的搜索输入字段,这就是为什么它会传递<input type="text" name="search[]" placeholder="Search..." />

text_field_tag

改为使用<%= text_field_tag :search, params[:search], placeholder: "Search..." %>

IDENTITY_INSERT