简体   繁体   English

Ransack Ruby On Rails结果无法渲染

[英]Ransack Ruby On Rails Result Wont Render

I have been trying to implement the Ransack search gem into a project I am currently working on. 我一直在尝试将Ransack搜索gem实施到当前正在研究的项目中。 I am pretty sure that I have it set up correctly. 我很确定自己设置正确。 I want to be able to search profiles and have placed my code in the profiles controller like so: 我希望能够搜索配置文件并将我的代码放置在配置文件控制器中,如下所示:

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

And the profile index.html.erb file like so: 概要文件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 %>

It does at the least appear to be attempting to search database correctly but just wont render any results on screen. 它至少看起来确实试图正确搜索数据库,但不会在屏幕上呈现任何结果。 It might be something obvious I am just not seeing. 我可能没有看到,这很明显。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Try a set up like this in your controller and view: 在您的控制器中尝试这样的设置并查看:

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

Is the problem when you try to access @profile or just is it with knowing how to display results? 是尝试访问@profile时出现的问题,还是只是知道如何显示结果? If the latter, right now they are being stored in @profile so you need to loop through them and choose what you want to show from each profile instance. 如果是后者,那么现在它们将存储在@profile中,因此您需要遍历它们并从每个概要文件实例中选择要显示的内容。

From when I've used this gem it seems it defaults to the index page of the given class (so in your case, the profile index page). 从我使用此gem以来,它似乎默认为给定类的索引页面(因此,在您的情况下为个人资料索引页面)。 So you can either: 因此,您可以:

  1. Just let the results list below the search form you shared above. 只需让结果列表在您上面共享的搜索表单下方即可。
  2. Move the search bar to something like a homepages controller so when a search is made it'll go to the profile index page and only show the results. 将搜索栏移动到主页控制器之类的位置,以便在进行搜索时将转到个人资料索引页面,仅显示结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM