I have a very simple Rails app on which I want to use Ransack search.
I get the following error though:
undefined method `name_cont' for Ransack::Search<class: Post, base: Grouping
<combinator: and>>:Ransack::Search
I only need the basics and as per instructions in my posts controller and posts index page I put in the following code:
Posts controller:
def index
@q = Post.search(params[:q])
@posts = @q.result(:distinct => true)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
Index View:
<%= search_form_for @q do |f| %>
<div class="field">
<%= f.label :name_cont, "Name contains" %>
<%= f.text_field :name_cont %>
</div>
<div class="actions">
<%= f.submit "Search" %>
</div>
<% end %>
I believe your issue is that the method name_cont
has not been defined in your controller. I believe whats happening here is that the search form gets an object that doesn't have name_cont
defined which is why your getting this error. My suggestion is to define a variable name for searching the post.