Ruby on Rails: Advanced search
You can create a new controller called search
.
Your search form:
<%= form_tag search_index_path, method: :get do %>
<%= text_field_tag :project, params[:project] %>
<%= text_field_tag :client, params[:client] %>
<%= submit_tag "Search", name: nil %>
<% end %>
incude in your routes.rb:
get "search/index"
your search controller:
def index
#store all the projects that match the name searched
@projects = Project.where("name LIKE ? ", "%#{params[:project]}%")
#store all the clients that match the name searched
@clients = Client.where("name LIKE ? ", "%#{params[:client]}%")
end
Now you can play with @projects
and @clients
in the index view.
Just be careful, because these variables might became nil if there is no match for the search.
EDIT - I am assuming you have two models Project
and Client
- if you cannot create a new controller you can create the search action in your current controller.
def search
#store all the projects that match the name searched
@projects = Project.where("name LIKE ? ", "%#{params[:project]}%")
#store all the clients that match the name searched
@clients = Client.where("name LIKE ? ", "%#{params[:client]}%")
end
And than you can use the @projects
and @clients
in the search view.
If you are trying to display the results in somewhere else (for example index
view), you can just move the above to the correct action.
def index
....
#store all the projects that match the name searched
@projects = Project.where("name LIKE ? ", "%#{params[:project]}%")
#store all the clients that match the name searched
@clients = Client.where("name LIKE ? ", "%#{params[:client]}%")
end
EDIT 2 - OK, you are trying to search by a combination of fields in the same model:
You and change your search method to add these two fields:
def self.search(search_project, search_client)
return scoped unless search_project.present? || search_client.present?
where(['project_name LIKE ? AND client LIKE ?', "%#{search_project}%", "%#{search_client}%"])
end
But please note the ||
will return scope if your search_project OR search_client are not present, you can change for AND (&&) if you prefer.
Also, the AND
will return only if both match, I mean the combination of search... You can also change it to OR
if you want.
Having the search form:
Your search form:
<%= form_tag search_index_path, method: :get do %>
<%= text_field_tag :project, params[:project] %>
<%= text_field_tag :client, params[:client] %>
<%= submit_tag "Search", name: nil %>
<% end %>
Then your controller must send the combination to the model:
@project_search = Project.search(params[:project], params[:client]).all
I think it will solve the problem...
Ruby on Rails: Advanced search with two string in one search
Use like the below:
keywords = 'test'
with AND:
Book.where("name LIKE '%#{keywords}%' AND category LIKE '%#{keywords}%'") if keywords.present?
with OR:
Book.where("name LIKE '%#{keywords}%' OR category LIKE '%#{keywords}%'") if keywords.present?
But this usage is not safe. Read following warning from Rails documentation:
Building your own conditions as pure strings can leave you vulnerable
to SQL injection exploits. For example, Client.where("first_name LIKE
'%#{params[:first_name]}%'") is not safe.
Advanced Search Box Not Showing Results or Connecting to the Show View
I was able to fix it.
On the new.html.erb
<section class="pb_section bg-light pb_slant-white pb_pb-250" id="section-features">
<div class="container">
<div class="row align-items-center justify-content-center">
<div class="col-md-12 relative align-self-center">
<form action="/search" class="bg-white rounded pb_form_v1">
<h2 class="mb-4 mt-0 text-center">Advanced Search</h2>
<hr>
<%= render 'form', search: @search %>
<hr>
<%= link_to 'Back', books_path %>
</form>
</div>
</div>
</div>
</section>
I simply changed the form tag to to a div tag, and also removed the action attribute from the form tag.
I have learnt a lot in the process.
That's all.
I hope this helps
Creating advanced search options
You can setup your form however you like. The key is you need to then convert your form parameters into a search options Hash to pass to ThinkingSphinx.
For example, for indexes you will specify conditions:
User.search(query, :conditions => { :ethnicity => params[:ethnicity], :religion => params[:religion] })
For the 'has' attributes you would specify :with as the search option. Also, you should probably add age as a 'has' attribute so you can use Sphinx to do the max computation like so:
User.search(query, :with => { :age => 20..65 })
See also: http://pat.github.io/thinking-sphinx/searching.html
Related Topics
How to Write Postgresql Functions on Ruby on Rails
Learning Insertion Sort in Ruby
How to Switch to Ruby 1.9.3 Installed Using Homebrew
Why Can't I Install Rails on Lion Using Rvm
Heroku Not Sending Email With Gmail Smtp
Find Key/Value Pairs Deep Inside a Hash Containing an Arbitrary Number of Nested Hashes and Arrays
How to Deal With the Sum of Rounded Percentage Not Being 100
How to Wrap Link_To Around Some HTML Ruby Code
How to Use Bundler Behind a Proxy
Rails4 Unknown Encoding Name - Cp720
Ruby on Rails - Access Controller Variable from Model
Where Is the Rails Method That Converts Data from 'Datetime_Select' into a Datetime Object
How to Use Global Variables or Constant Values in Ruby
Find Unused Code in a Rails App
How to Call a Controller'S Method from a View (As We Call from Helper Ideally)