Ransack search form in the header part: No Ransack :: A search object was provided by search_form_for

First of all, I'm new to RoR, so the answer may be obvious, in which case I apologize. I looked around and did not find anything useful.

I am trying to find a search form in the title of every web page in my application that will search for the names of all my buckets. Here is the relevant code:

In the application / views / layouts / _header.html.erb (in the navigation bar):

<% search_form_for @q do |f| %> <%= f.label :name_cont %> <%= f.text_field :name_cont %> <%= f.submit %> <% end %> 

In application / controllers / buckets_controller.rb:

 def index unless params[:q].blank? @q = Bucket.search(params[:q]) @buckets = @q.result.paginate(:page => params[:page]) else @buckets = Bucket.find(:all, :limit => 5).paginate(:page => params[:page]) end end 

I understand that the last part is not so great: I try to do it, if I just get access to the bucket index page (not by searching), I show the last 5 buckets created. When I search for something in the form of a title, I go to the index page, but show only those buckets that fall into the search. (is it better to process it so that the search page is separate from my index page?)

I found this problem , which is pretty much identical, but I still don’t see how I handle @q , if every page goes to have a form on it - of course I don’t need to change each controller for every action?

Sorry in advance for any disappointment that I can call my love because you do it!

+7
source share
3 answers

As others have said, you need to use ApplicationController before_filter. Although ernie himself does not seem to recommend this, the implementation is simple.

First use the advanced Ransack options to specify the search path.

 #config/routes.rb resources :buckets do collection do match 'search' => 'buckets#search', via: [:get, :post], as: :search end end 

Secondly, upgrade your BucketsController to include the following custom action:

 #controllers/buckets_controller.rb def search index render :index end 

Nothing special. If you are currently trying to search, you will receive an error message from the original question. Your q variable definition is correctly implemented, but you will have to move it to ApplicationController as follows:

 #controllers/application_controller.rb before_filter :set_global_search_variable def set_global_search_variable @q = Bucket.search(params[:q]) end 

Finally, update your search form to submit the correct search parameters

 #layouts/_header.html.erb <% search_form_for @q, url: search_buckets_path, html: { method: :post } do |f| %> <%= f.label :name_cont %> <%= f.text_field :name_cont %> <%= f.submit %> <% end %> 
+9
source

No, you do not need to edit all your controllers.

You can use the ApplicationController for all of your "general" controller needs. Read it in the guides http://guides.rubyonrails.org/action_controller_overview.html and the API docs http://api.rubyonrails.org/classes/ActionController/Base.html

The key is here, when you created the new rails application, you will notice that it created the file ... / app / controllerlers / action _controller.rb, and this class comes from ActionController :: Base. Then, if you use the rail generator again to create a controller for your application, you will notice that your new controller class comes from ApplicationController (not :: Base). This means that application_controller.rb is the parent controller class for your application. This means that everything in it is available to all your application controllers. Easy to abuse, so be careful.

+1
source

It seems like this is impossible. This is a commentary by the author of the book Ernie.

You will need to handle the required Ransack stuff in the before_filter or (ick) file in a partial view. If you put a search box on every single part of the site, I would recommend that you strongly consider whether ransack is the right tool for the job. You might want something like an inverted index lookup such as sphinx, solr, etc.

https://github.com/ernie/ransack/issues/3

+1
source

All Articles