Ask A Question

Notifications

You’re not receiving notifications from this thread.

Forum Series Part 6: Search with Ransack Discussion

Thanks for the great video Chris!

Does ransack support autocomplete while typing in a search query? I checked out the documentation on github, but it doesn't mention anything about it.

Also, what do you think about Searchkick (https://github.com/ankane/s... Would love to see a video on this too!

Thanks again!

Reply

You can do autocomplete by having it query the URL for JSON response each time someone types a character. My suggestion is to try hooking it up with jQuery UI's autocomplete. http://jqueryui.com/autocom...

Reply

Just implemented this in conjunction with Ryan's episode 370 http://railscasts.com/episo.... Have some really cool advanced search features now working on my customer index page.

Now just need to figure out how to rspec this stuff (I find BDD doesn't work in the learning phase).

Reply
Kohl Kohlbrenner Kohl Kohlbrenner

@excid3:disqus in the controller why isn't it ForumThread.search(params[:subject_cont]) ?

Reply

You need to pass in params[:q] so it passes in both the name of the field and the search you want to do on it. Since it supports multiple fields in search, you pass them all in instead of just one.

Reply
Kohl Kohlbrenner Kohl Kohlbrenner

@excid3:disqus Do you know how to include a has_many through relationship in the search? Specifically, I'd like to search for tags associated with a recipe. The setup is similar to acts_as_taggable but I rolled my own so to speak. I tried :name_or_category_or_tags_cont but got an error.

Reply

It looks like on the Ransack docs, they do this:

class Artist
has_many :memberships
has_many :musicians, through: :memberships
end

artists = Artist.ransack(musicians_email_cont: 'bar')

Reply

Hey Chris! Nice one! I'm already using ransack gem based on this tutorial. On the top of this for some models in my app I would like to take this one step further by using some AJAX/jQuery autocomplete search/sorting for my table. I found a bunch of things, but every given solution I found seemed to be outdated. As of Nov 2015, could you tell me what gems/jQuery plug-ins/etc I should use to implement both the ajaxified searching and sorting feature that will be good enough in production? Thanks!

Reply

Great video! I want to download the search results into an Excel file and am able to kind of do this. I'm having 2 problems.

First, when I use the @projects variable below, it works but only allows me to download 40 results as per the will_paginate gem.

@projects = @q.result.includes(:vendor, :service_category).paginate(:page => params[:page], :per_page => 40)

Second, I have a standard download (with many tabs) that is not dependent on the search results. I want there to be two exports on the projects index page- one which is the standard spreadsheet and the other that only includes the search results. Can I do this in the index action?

This is what I have in my respond_to block

format.xls { response.headers['Content-Disposition'] = "attachment; filename=\"projects_list_created_on_#{formatted_date_today}.xls\""
and there is a corresponding index.xls.erb file in the views.

Thank you in advance for any advice!

Reply
Brian Gilbank Brian Gilbank

How would you add ransack if your post controller looks like this?

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: 2)
end

Reply

Hi Chris, I added Ransack to search for users and the search field always is pre-filled with the logged-in user's email and has to be deleted manually. Before a search the field is pre-filled again. Any hint on what's going on?

Reply

Do I need to have eelastic search installed for this setup or searchkick?

I basically want to get this to work : https://gorails.com/episodes/global-autocomplete-search?autoplay=1

By following this tutorial will I have the setup needed to do the above link as well?

Reply

Ransack is really cool!

Reply
Join the discussion
Create an account Log in

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

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

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