1
我使用簡單的搜索表單和Heroku的全文搜索說明跟蹤RailsCast,並使用pg_search gem。使用作用域kaminari分頁進行全文搜索
我試圖通過我的microposts搜索,同時保留我爲我的索引設置的範圍。
這裏是我的microposts_controller.rb索引操作
def index
#@microposts = Kaminari.paginate_array(Micropost.popular).page(params[:page]).per(25)
@micropost = current_user.microposts.build
@microposts = Micropost.search(params[:query]).page params[:page]
end
註釋掉的代碼是我的代碼,我想的範圍。它自行工作,但無法搜索。 @microposts的新分配適用於搜索,但它使我的索引頁面失去了我爲它設置的'流行'範圍。
這裏是微柱的index.html.erb
<%= form_tag microposts_path, method: :get do %>
<%= text_field_tag :query, params[:query], placeholder: 'Search for a micropost' %>
<%= submit_tag "Search", name: nil, class: "btn btn-lrg btn-primary" %>
<% end %>
<%= render 'shared/public_feed' %>
_public_feed.html.erb
<% if @microposts.any? %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: @microposts %>
</ol>
<% end %>
<%= paginate @microposts, :theme => 'twitter-bootstrap', :pagination_class => "pagination-sm" %>
micropost.rb
def self.popular
reorder('votes desc').find_with_reputation(:votes, :all)
end
我不在乎,如果搜索結果具有「受歡迎」的作用範圍,我只希望對索引頁面進行初始訪問,以便在搜索之前擁有「受歡迎」範圍ch執行。我在想也許有一個if語句可以放在控制器或視圖中的某處,但似乎沒有任何工作。