簡體   English   中英

Ransack Ruby On Rails結果無法渲染

[英]Ransack Ruby On Rails Result Wont Render

我一直在嘗試將Ransack搜索gem實施到當前正在研究的項目中。 我很確定自己設置正確。 我希望能夠搜索配置文件並將我的代碼放置在配置文件控制器中,如下所示:

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 %>

它至少看起來確實試圖正確搜索數據庫,但不會在屏幕上呈現任何結果。 我可能沒有看到,這很明顯。 任何幫助是極大的贊賞。

在您的控制器中嘗試這樣的設置並查看:

 #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 %>

是嘗試訪問@profile時出現的問題,還是只是知道如何顯示結果? 如果是后者,那么現在它們將存儲在@profile中,因此您需要遍歷它們並從每個概要文件實例中選擇要顯示的內容。

從我使用此gem以來,它似乎默認為給定類的索引頁面(因此,在您的情況下為個人資料索引頁面)。 因此,您可以:

  1. 只需讓結果列表在您上面共享的搜索表單下方即可。
  2. 將搜索欄移動到主頁控制器之類的位置,以便在進行搜索時將轉到個人資料索引頁面,僅顯示結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM