Search form for Paper_trail history using Ransack gem

时间:2016-04-07 10:41:55

标签: ruby-on-rails

I'm struggling to get a search form for my active record history. I'm using the paper_trail gem as well as ransack. Both, alone, are working well. But whenever I try to implement a search form I get a strange error:

undefined method `paper_trail_versions_path' for #<#:0x007fede31da910>

This line from my views gets highlighted:

<%= search_form_for @q, html: {class: "input-group"} do |f| %>

Here's the code of the controller:

def history
 @all_versions = PaperTrail::Version.order('created_at DESC')
 @versions = @all_versions.paginate(:page => params[:page], :per_page => 100)
 @q = @versions.search(params[:q])
 @versions = @q.result
 respond_to do |format|
    format.html
  end
end

the views:

<%= search_form_for @q, html: {class: "input-group"} do |f| %>
  <%= f.search_field :whodunnit_or_item_id_cont, placeholder: "Search for Users or Actions..", class: "form-control" %>
  <span class="input-group-btn">
    <%= button_tag(type: 'submit', class: "btn btn-default") do %>
    <i class="fa fa-search"></i>
    <% end %>
  </span>
<% end %>

Any Ideas?

Thanks in advance!

EDIT resolved, thanks to answer:

<%= search_form_for @q, html: {class: "input-group"}, :url => "/dashboards/history/" do |f| %>

1 个答案:

答案 0 :(得分:1)

您看到的错误来自search_form_for助手尝试猜测表单操作的网址。

Under the hood,Ransack使用polymorphic_path rails helper,即according to the doc,一种方法

  

在给定Active Record模型实例

时智能解析到命名路由调用

在您的情况下,您的模型为PaperTrail::Version,可解析为paper_trail_versions_path

要修复错误,您可以:

  • routes.rb文件中定义相应的资源,以便定义帮助程序
  • 手动定义帮助
  • 在搜索表单中提供url选项,其中包含表单操作的路径。

希望它有所帮助!