我正在使用 ransack 提前搜索我的一个项目。我面临一个问题,如果我进行搜索并保存查询,我将无法使用这些查询再次重建搜索表单。我正在寻找的是,当我进行搜索时,我应该能够再次看到相同的表单,其中包含相同的值。
布局/_search.html.erb
<%= search_form_for @search, url: url, method: :post, class: search_form_class, remote: true do |f| %>
<%= f.condition_fields do |c| %>
<%= render "layouts/condition_fields", f: c %>
<% end %>
<%= f.submit %>
<% end %>
布局/_condition_fields.html.erb
<div class="field">
<%= f.attribute_fields do |a| %>
<%= a.attribute_select %>
<% end %>
<%= f.predicate_select %>
<%= f.value_fields do |v| %>
<%= v.text_field :value %>
<% end %>
<%= link_to "remove", '#', class: "remove_fields" %>
</div>
people_controllers.rb
def index
@search = Person.ransack(params[:q])
@people = @search.result(distinct: true)
@search.build_condition
respond_to do |format|
format.html
end
end
人/index.html.erb
<%= render :partial => 'layouts/search', :locals => {:search => @search, :url => search_people_path, search_form_class: 'all_person'} %>
<div class="searchResult">
<%= render :partial => 'layouts/search_result', :locals => {:People => @people} %>
布局/_search_result.html.erb
<div class="box">
<div class="box-body">
<table class="table table-bordered">
<tbody>
<tr>
<th>id</th>
<th>name</th>
<th>email</th>
</tr>
<% @people.each do |person| %>
<tr>
<td>
<%= person.id %>
</td>
<td>
<%= person.name %>
</td>
<td>
<%= person.email %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
这就是现在当我去查看时它出现的地方。
看起来有人 在这里问类似的问题!
我完全想要同样的东西。
- 如何保存表单搜索值并保存这些详细信息以供以后查询
- 通过采用现有查询生成表单来编辑保存的查询