Search form with checkboxes - ruby-on-rails

Im trying to create a form that finds products with checkboxes. I think the problem lies in the fact that im not passing an array to my controller. Does anyone know how to fix this?
Model
def self.search(params)
arel = order('created_at DESC') # note: default is all, just sorted
arel = arel.where('name LIKE ?', "%#{params[:search]}%").order('created_at DESC') if params[:search].present?
arel
end
Controller
def index
#products = Product.search(params)
end
View
<%= form_tag(products_path, :method => "get", id: "search-form") do %>
<%= check_box_tag :search, "product1", nil %>
<%= check_box_tag :search, "product2", nil %>
<%= submit_tag "Search" %>
<% end %>

Both checkboxes are the same :search so only the last checked one is being sent. Try something like this:
<%= check_box_tag "search[]", "product1" %>
<%= check_box_tag "search[]", "product2" %>

Related

pagination ruby on rails with will_paginate

Hey guys I am trying to implement pagination using the will_paginate gem, and it doesn't seem to be working. I have added the gem to my Gemfile and tried the code below:
controller
def team_search
#teams = CareTeam.all.order('created_at ASC').paginate(page:params[:page], per_page: 10)
end
view
<h1>Search for a team</h1>
<p>search for a team by team name</p>
<%= form_tag search_team_results_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search], placeholder:
"Search Teams By name", required: true %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<%= will_paginate #teams %>
model
def self.search(team_name)
if team_name
team_name.downcase!
self.where('LOWER(team_name) LIKE ?', "%#{team_name}%").order('created_at ASC')
else
CareTeam.all.order('created_at ASC')
end
end
When I navigate to my view, nothing shows up. Does anyone analyse from above what could be wrong?

Rails 5: search by keyword and sort by distance

I have a search form on my app:
<%= form_for search_path, method: :get do |f| %>
<p>
<%= f.label "Search for" %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "search", name: nil %>
</p>
<% end %>
that searches by distance:
def search
if params[:search].present?
#las = La.near(action,10).reorder('distance')
else
#las = []
end
end
The results are sorted by distance and all works well up to here!! The only issue here is that the results do not appear according to the keyword typed as well. Thus whatever keyword I type, all results appear and sorted by distance.
Any idea what I might be doing wrong here??
You could try passing the content of params[:search] within the near scope:
#items = Item.near(params[:search], 10).reorder('distance')

Rails - Simple search function

I'm fairly new to rails and am building an online shop just to write some rails. I'm in the process of implementing a simple search function at the moment and get some strange behaviour I can't explain.
Model method:
def self.search(query)
where("title like ?", "%#{query}%")
end
Controller methode:
def index
if params[:search]
#products = Product.search(params[:search])
else
#products = []
#Only lists available products (in cart counts as available)
#available_items = Item.where(user_id: nil).select(:product_id).uniq
#available_items.each do |item|
#products << item.product
end
end
end
Search form:
<%= form_tag(products_path, :method => "get", id: "search-form", enforce_utf8: false) do %>
<%= text_field :search, params[:search], placeholder: "Search..." %>
<%= submit_tag "Search", :name => nil %>
<% end %>
When I trigger a search I get no results and the url looks like this:
http://localhost:3000/products?search%5B%5D=Paper
When I remove '%5B%5D' from the url it all works fine and I get my results. '%5B%5D' stands for '[]' in URI encoding, can't figure out where that come from though. Any help would be greatly appreciated!
Best,
Paul
See reference how text_field and reference for text_field_tag helper works:
<%= text_field :search, params[:search], placeholder: "Search..." %>
It will give you search input field with name=search[], thats why its passing search[]='text'.
<input type="text" name="search[]" placeholder="Search..." />
Use text_field_tag instead:
<%= text_field_tag :search, params[:search], placeholder: "Search..." %>

Merge link and select params

In my case i have link with params and select with params
If I filter #posts with select (:per_page) and then with (:pub) params everything is ok, because I used params.merge in link.
But now if I want to use first link and then select, it does not work, because I don't know where I should write params.merge in my select.
Code from my controller:
def index
#posts = Post
#posts = #posts.published unless params[:pub]
#posts = #posts.where(:published => params[:pub]) if params[:pub]
#posts = #posts.page(params[:page]).per(params[:per_page] || 5)
end
code from view
<%= select_tag :per_page, options_for_select(%w(1 2 3), params[:per_page].to_i), :onchange => "if(this.value){window.location='?per_page='+this.value;}" %>
<%= link_to "unpubl", params.merge(:pub => :f) %>
<%= link_to "publ", params.merge(:pub => :t) %>
If I understood correctly you are trying to create some kind of HTML filters to pass to your controller right ?
If this is the case, you should build your HTML filters using a form. This way you would not have to deal with "merging" your options into the params hash. The form will take care of that for you.
Example:
<%= form_tag posts_path, method: :get %>
<%= select_tag :per_page, options_for_select(%w(1 2 3), params[:per_page].to_i) %>
<%= check_box_tag 'published', 'true' %>
<%= submit_tag 'Submit' %>
<% end %>
Then in your controller:
params[:published]
params[:per_page]
will hold the corresponding values.
More information about form_tag here

Jquery Tokeninput & Acts-as-taggable is not working with a parent-child-child nested form

I have form which is built like this:
<%= form_for #location do |f| %>
<%= f.fields_for :product_dates do |d| %>
<%= d.fields_for :products |p| %>
<%= p.text_field :tag_list,"data-pre" => #product.tags.map(&:attributes).to_json %>
Now when i go to the page i get an error when using the line: "data-pre" => #product.tags.map(&:attributes).to_json which is undefined method tags for nil:NilClass but everything is fine when i take it away. This some type of TokenInput bug? Anyone else had to deal with this?
ProductsController:
def new
#location = Location.new
product_date = #location.product_dates.build
product_date.products.build
end
You simply didn't set your #product variable => it's nil.
You should show your controller
EDIT:
replace:
<%= p.text_field :tag_list,"data-pre" => #product.tags.map(&:attributes).to_json %>
with:
<%= p.text_field :tag_list,"data-pre" => p.object.tags.map(&:attributes).to_json %>
This should work for edit as well.
It's really good sense here: you can't invoke something you didn't set.

Resources