Rails 4 Search By Category -


I want to place search form on my homepage on # Home pages for itemprop = "text"> I want to find my model by category. I am unable to find a solution, where you are actually putting arguments on different controllers compared to those models that you are looking for. I was thinking that someone can help in syntax and where to go it. Here are my connections:

vendor.rb

  class vendor & lt; ActiveRecord :: Base Confirms: Category_ID, Attendance: true true_to: end of range  

category.rb

  class Category & lt; ActiveRecord :: Base is_on: vendor end  

pages_controller.rb

  def home @ seller / vendor.You  

routes.rb

  root 'page # home'  

I'm trying To insert a search form at home.html.erb, which is under the page layout. I was hoping someone could help me how I could accomplish this. It seems (this seems) a simple type of discovery, I hope no gem should be used.

Edit: Answer for those searches, thanks @Vla

Vendors_controller. RB

  def search @ vendors = vendor Search term (parameter)  

pages / home.html. ARB (this is my root 'Page # Home')

  <% = form_tag vendors_search_path, method :: get Do | F | & Gt%; & Lt;% = text_field_tag: search, parameter [: search], placeholder: "Find vendors"% & gt; & Lt;% = select_tag 'category', options_for_select (category, all .mapp {| el | [el.name, el.id]})% & gt; & Lt;% = submit_tag 'search'% ' & Lt;% end% & gt;  

routes.rb (be sure to keep it near the top)

  get 'vendors / search'  

Sellers / search.html.erb

  & lt;% @ sellers. Seller | & Gt%; & Lt;% = seller.name% & gt; & Lt;% = seller.phhone% & gt; & Lt;% = seller.email% & gt; & Lt;% end% & gt;  

OK, you can do this:

Add your vendor.rb file and the search method like this:

  def self.search params Seller = Seller Where (category_ id: parameters [: category] .to_i) Parameter [: Category] .blank? End of vendors  

Then create a search method in your vendors_controller:

  def search @ vendors = seller. Search (Parameter) End  

Then create a form on the homepage like this:

  = form_tag search_vendors_path, Method :: Receive, Role: Make 'Form' . Format group% label category = selection_tag 'category', options_for_select (category.all.map {| el | [el.name, el.id]}), category: 'form-control' = submit_tag 'search'  

Do not forget and enter

  'vendors / search'  

and "search" view in your way.

You can still do with the results on your homepage, without search operation. Anyway I hope you get this idea.


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -