Reputation: 19
I am setting up a search functionality in my Ruby On Rails application. I want users to search for a particular topic and be able to display the results. However, if there is no results pending, it should display a validation.
I have currently tried researching and adding different code such as adding @topic.paginate(:page => 1, :per_page => 2)
and
trying this:
Topic.tagged_with(params[:tag]).order(created_at: :desc).page(params[:page]).per(3)
topics_controller:
def index
if params[:search].present?
@topics = Topic.search(params[:search]).paginate(:page => params[:page], :per_page => 5)
flash[:notice] = "No records found based on the search." if @topics.blank?
else
@topics = Topic.all
flash[:notice] = "No records found in Database." if @topics.blank?
end
end
index.html.erb:
<div>
<%= will_paginate @topic, renderer: BootstrapPagination::Rails %>
</end>
I expect as a validation message to appear. But instead, I have the following error:
**undefined method `total_pages'
I understand this error because of the pagination being added but not sure how to overcome this.
Upvotes: 0
Views: 445
Reputation: 15838
Maybe the problem is not your pagination but the else
branch where you are not paginating @topics
. Will_paginate can't create the pagination links.
def index
if params[:search].present?
@topics = Topic.search(params[:search]).paginate(:page => params[:page], :per_page => 5)
flash[:notice] = "No records found based on the search." if @topics.blank?
else
@topics = Topic.all.paginate(:page => params[:page], :per_page => 5) #add this
flash[:notice] = "No records found in Database." if @topics.blank?
end
end
Upvotes: 1