I'm using sunspot for the first time and i'm trying to setup the search. full text search seems to work fine. however, i have a form with a search box and multiple filters on boolean fields that the user can select. somehow the search box works fine but solr isn't picking up the individual booleans as additional filters. also, when i don't do any search text and just want to search by the boolean fields, nothing happens. any help would be appreciated:
this is my controller:
#search = Project.search do
fulltext params[:search]
facet(:master_bedroom)
facet(:dining_room)
facet(:bath)
with(:master_bedroom, params[:mb]) if params[:mb].present?
with(:dining_room, params[:dr]) if params[:dr].present?
with(:bath, params[:p_bath]) if params[:p_bath].present?
end
i have the fields in the model:
searchable do
text :description
boolean :dining_room
boolean :bath
boolean :master_bedroom
end
and i have the following for my view:
<%= form_tag projects_path, :method => :get do %>
<%= text_field_tag :search, params[:search] %>
<%= check_box_tag :bath, 'true'%>
<%= submit_tag "Search", :name => nil %>
<% end %>
There was an error in variable naming.
Related
I'm getting a little stuck with a ransack search form.
I need to search through a model's association, and bring results back when name_cont BUT ONLY WHEN AN associated model's status == something.
I have a gig model with a has_many belongs_to relationship with a request model and user model. The request model has attributes status and the user model has attributes publicname. I want to search publicname_cont and only show results from the requests that have a status == "hired".
Currently my search brings back the results of publicname regardless or the request models status.
Gigs Controller:
#gigs = #q.result(distinct: false).notexpired.order(date: :asc).page(params[:page]).per(20)
Search form:
<%= search_form_for #q, url: welcome_index_url, remote: false do |f| %>
<%= text_field_tag :search, params[:search], class: 'loc-search', placeholder: "Location"%>
<%= f.search_field :locationname_cont, placeholder: "Bar name" %>
<%= f.search_field :requests_user_publicname_cont, placeholder: "Band name" %>
<%= f.submit %>
<% end %>
Currently, I can search requests_user_publicname_cont and it gives me all the results as expected where the parameters match.
I am looking for a way to search requests_user_publicname_cont AND requests_status_eq, ("hired")
I am not sure how to go about this, any help would be appreciated, thanks.
#q = Gig.joins(:requests).where(requests: { status: 'hired' }).ransack(params[:q])
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..." %>
I have a simple search working in my rails app, but it only searches a single column of my table. I'm trying to add a simple dropdown menu with options like "last_name" and "city" so the user can select the column to search.
In my view I have:
<%= form_tag teachers_path, :method => 'get', :id => "teachers_search" do %>
<%= hidden_field_tag :direction, params[:direction]%>
<%= hidden_field_tag :sort, params[:sort]%>
<p>
<%= text_field_tag :search, params[:search], placeholder: 'First Name' %>
<%= submit_tag "Search", :first_name => nil %>
</p>
<% end %>
and in my model:
def self.search(search)
if search
where('first_name LIKE ?', "%#{search}%")
else
scoped
end
end
Any help greatly appreciated.
You can add a select_tag for your drop down menu
<%= select_tag "search_from", "<option>first_name</option><option>last_name</option><option>city_name</option>".html_safe%>
In your controller you can pass the value in params[:search_from] to your search method. and modify it to include the column name
def self.search(search, column_name)
if search
where('? LIKE ?', column_name, "%#{search}%")
else
scoped
end
end
I've written it in a crude way but i hope it gets the message along.
extending #sohaibs's answer dropdown is a good idea if you are only allowing user to filter result with some some fixed attributes
views
<%= select_tag "search_by", options_for_select(['first_name','last_name','city'].map{|o| [o.humanize,o] }) %>
<%= f.text_field 'search' %>
controller
def search
User.search params[:teacher][:search_by],params[:teacher][:search]
end
and model
def self.search(search_by,search_value)
where('? LIKE ?', search_by, search_value)
end
Awesome Rails :)
Have you tried:
def self.search(column, search)
# some logic
where('? LIKE ?', column, "%#{search}%")
# some logic
end
I started using Sunspot to perform searches in my Rails 3 app and I ran into a doubt. Is there a way that I can let the user choose in what fields he/she wants to search. For example, in my app we have:
class Project < ActiveRecord::Base
searchable do
text :name, :content, :keyword
end
end
And in the View the default search bar:
<%= form_tag projects_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
Can I add a radio button or something like that so the user can mark it in order just to search by name, or content or keyword? If yes, how can I make it?
Thanks a lot.
Watch the railscasts on the subject: http://railscasts.com/episodes/278-search-with-sunspot .. in it, Ryan lets user's query optionally by month.
So, if month was sent in:
def index
#search = Article.search do
fulltext params[:search]
with(:publish_month, params[:month]) if params[:month].present?
end
#articles = #search.results
end
I got a list page and I filter items via links with get params (I can choose many links so query would be like "?param1=value1¶m2=value2"). But also I have to filter it by text field, so I made a form:
<form>
<%= text_field_tag :zip, params[:zip] %>
<%= submit_tag 'OK', :name => nil %>
</form>
But when I submit it, text field param replaces existing query params. So, how to make text field value add to query, not to replace it?
Since I was just dealing with this problem in Rails 4 I thought I'd share my solution.
My page gets loaded with a sport_id parameter, and when the user specifies a sort-order I wanted it to submit a GET request for page.url/event?sport_id=1&sortby=viewers but it wouldn't preserve the sport_id parameter until I added a hidden field tag in the form like so:
<%= hidden_field_tag :sport_id, params[:sport_id] %>
This solution does submit an empty sport_id parameter if that parameter was not in the original request, but that is easily prevented by encapsulating the hidden field in an <% if params[:sport_id].present? %> condition.
Use hidden_field_tag.
Inside of your form, just set hidden_field_tags for the existing GET params, like so:
<% request.query_parameters.collect do |key, value| %>
<%= hidden_field_tag key, value %>
<% end %>
This will ensure that your existing params persist.
Rails 3?
<%= form_tag your_path(params.except(:controller, :action)), :method => :get do %>
<%= text_field_tag :zip, params[:zip] %>
<%= submit_tag 'OK', :name => nil %>
<% end %>