2016-11-16 64 views
0

我一直在嘗試將Ransack搜索gem實現到我目前正在使用的項目中。我很確定我已經正確設置了它。我希望能夠搜索個人資料和已經把我的代碼在配置文件控制,像這樣:Ransack Ruby On Rails結果Wont Render

def index 
@q = Profile.search(params[:q]) 
@profile = @q.result(distinct: true) 
@profiles = Profile.all 
end 

和輪廓index.html.erb文件,像這樣:

<%= search_form_for @q do |f| %> 
<div class="field"> 
<%= f.label :first_name_cont, "Name Contains" %> 
<%= f.search_field :first_name_cont %> 
</div> 
<div class="actions"><%= f.submit "Search" %></div> 
<% end %> 

它確實在似乎最少似乎試圖正確地搜索數據庫,但不會在屏幕上顯示任何結果。這可能是顯而易見的,我只是沒有看到。任何幫助是極大的讚賞。

回答

1

嘗試在你的控制器和視圖設置是這樣的:

#profile controller 
    def index 
    @search = Profile.search(params[:q]) 
    @profiles = @search.result(distinct: true) 
    end 

    #profile index 
    <%= search_form_for @search do |f| %> 
    <%= f.label :first_name_cont, "name in profile" %><br> 
    <%= f.submit "Search", class:"btn btn-info btn-block" %> 
    <% end %> 

    <% @profiles.each do |profile| %> 
    <%= profile.name %> 
    <% end %> 
+0

感謝古慕克,這並獲得成功。我對你給我的東西做了一些微小的修改。但它是非常重要的。非常感激。 –

0

當您嘗試訪問@profile或只知道如何顯示結果時,是否存在問題?如果後者,現在他們被存儲在@profile中,所以你需要遍歷它們並選擇你想要顯示每個配置文件實例。

從我使用這個gem時,它似乎默認爲給定類的索引頁面(所以在你的情況下,配置文件索引頁)。所以你可以:

  1. 只是讓結果列表在你上面分享的搜索表單下方。
  2. 將搜索欄移動到主頁控制器之類的東西,所以當搜索時,它會進入配置文件索引頁面,只顯示結果。