All threads / How do I add Ransack in my posts controller?

Ask A Question


You’re not receiving notifications from this thread.

How do I add Ransack in my posts controller?

Brian Gilbank asked in General

I am new to rails so this might be an easy question. I am trying to figure out the best way to add ransack to my posts index action.


@q = Post.ransack(params[:q])
@posts = @q.result(distinct: true)

Current Controller:

def index
if params[:category].blank?
@posts=Post.all.order("created_at desc").paginate(:page => params[:page], per_page: 15)
@category_id = Category.find_by(name: params[:category]).id
@posts = Post.where(category_id: @category_id).order("created_at DESC").paginate(:page => params[:page], per_page: 5)

Hey Brian,

For the most part you're going to just want to replace the Post.all and Post.where part with Ransack. You'd ideally pass in the category in to Ransack search instead as q[category_id_eq] in your UI.

Then you can say:

def index
  @q = Post.ransack(params[:q])
  @posts = @q.result(distinct: true).order(created_at: :desc)

  if params[:category].blank?
    @[email protected](:page => params[:page], per_page: 15)
    @posts = @posts.paginate(:page => params[:page], per_page: 5)

Just update your UI so that it sends over that category ID inside the q param so that Ransack can handle it correctly. If it's a link, you can change the param name there, or you can use the search form helper if it's a form you want for selecting the category.

Thanks Chris, I will try it out.

Just a quick follow up. How would I add searching for my posts show action?

I have a search form in my side bar on individual posts.

def show


Nothing too much. All you need to do is specify the @q variable so the search form knows what model it's searching. The results will go to your index action so there's nothing much you need to do here.

def show
    @q = Post.ransack(params[:q])

Hi Chris - I tried that and nothing happens for me unfortunately? It just stays on the same post, even if I search for a different post. In addition, I am currently using ransack search in my header, and I am receiving errors on my other controllers, such as: undefined method `title_or_body_cont' for #Ransack::Search:0x007f9da497a2b8.

Just wondering how I get around this. Should I add ransack to my application controller?

Also on a secondary note, if you could do a video on setting up active menu classes that would be awesome, I can't find a good solution!


You might need to set the URL on the form then so that it takes you to the index action. The search form may just normally only apply to the current url, I'm not sure.

Also that's a great idea. Like highlighting a link in the nav when you're on a certain controller right?

Yes, I have two different header styles and I can get menu highlighting on one but not both. There seems to be a lot of different options out there:


It's one of those things that needs to be pretty customizable at times because sometimes you might have nested routes and want to match the parent or something, not the controller or action.

There's a nifty little gem I noticed in the answers there. Looks like it would do a pretty good job allowing you to match most all of the variations you might need.

Good suggestion on the gem. It worked well, but it is highlighting both my home and about page at the same time:

Home and about are both show pages from my pages controller. I think this might be the reason, but I am not sure.

Any help would be awesome! This what I have:

<%= active_link_to "Home", root_path, :active => :exclusive %>
<%= active_link_to "About", pages_path, :action => "show", :id => 'about', :active => :exact %>

Yeah, your link for about doesn't actually point to /about, it just points to / which means they're the same. You should match on exact for both of those. It would look like this I think:

# config/routes.rb
get "about", to: "pages#about"
root to: "pages#home"
<%= active_link_to "Home", root_path, :active => :exact %>
<%= active_link_to "About", pages_path, :active => :exact %>

I believe exclusive would work as well. Doesn't really matter between those in this case.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 35,909+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.