0

我想显示搜索对象的搜索参数。我使用铁轨和太阳黑子。

假设我有这个书应用程序,其中存储了一堆书,并且想按它们的名称和标题搜索书。

图书型号:

class Book < ActiveRecord::Base
  validates_presence_of :title, :author_name

  searchable do
    text :title, :author_name
  end
end

控制器:

class BooksController < ApplictionController
  def index
   @search = Book.search do
    fulltext params[:search]
  end
   @results = @search.results
  end
end

看法:

<div>
 <h1>You search: </h1>
 <% for result in @results %>
    <li>
      <h3><%= link_to result.title, result %></h3>
      <p><%= result.author_name %></p>
    </li>
 <% end %>
</div>

如何显示搜索参数,该对象在哪里?

所以它输出:

Your search: Jeff Hawkins
Eggs and butter
Jeff Hawkins
4

1 回答 1

0

将您的控制器代码更改为:

def index
  @query = params[:search]
  @search = Book.search do
    fulltext params[:search]
  end
  @results = @search.results
end

然后在您看来,您可以这样做:

<h1>Your search: <%= @query %></h1>
于 2012-10-05T04:32:24.557 回答