I'm new to Ruby on Rails, and I'm creating my first site. I have a user model with 3 columns: firstname
, lastname
and email
. Im using ransack to make a search engine, and i want to have one search field that will go through every column and display the result.
I have made a user_controller.rb looking like this:
@q = User.search(params[:q])
@users = @q.result(:distinct => true)
And my User\index.html.erb looks like this:
<%= search_form_for @search do |f| %>
<div class="field">
<%= f.label :firstname_cont , "Firstname" %>
<%= f.text_field :firstname_cont %>
<%= f.label :lastname_cont , "Lastname" %>
<%= f.text_field :lastname_cont %>
<%= f.label :email_cont , "Email" %>
<%= f.text_field :email_cont %>
</div>
<div class="actions"><%= f.submit "Search" %></div>
<% end %>
Giving me 3 search fields, but i only want one that goes through firstname, lastname and email.
Edit 1 : After testing some more, I found out that the search field can't handle both firstname AND lastname, or any whitespaces. I have read that it is possible to make a helper_method like this: Searching multiple fields with Ransack
And when I get the search result I get thrown into my model index etc: user / index. Where can I change this?
But there is nothing on how to use it in the view. Why is it so hard to just whrigt, this is what you do :in Controller do this -> in model do this -> in view do this -> And dont forget to fixs the routes like this ->