Skip to main content

How do I add Ransack in my posts controller?

General • Asked by Brian Gilbank

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.

Ransack

@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)
else
@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)
end


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?
    @posts=@posts.paginate(:page => params[:page], per_page: 15)
  else
    @posts = @posts.paginate(:page => params[:page], per_page: 5)
  end
end

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. https://github.com/activerecord-hackery/ransack#ransacks-search_form_for-helper-replaces-form_for-for-creating-the-view-search-form


Thanks Chris, I will try it out.
Brian


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
end

Thanks,
Brian


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])
end

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!

Brian


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: http://stackoverflow.com/questions/3705898/best-way-to-add-current-class-to-nav-in-rails-3

Brian


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. https://github.com/comfy/active_link_to 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: https://mirrorcommunications.herokuapp.com/

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.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.