Ruby on Rails ransack gem search - ruby-on-rails

i want to implement a search that goes through multiple models.
Found this stackoverflow question here that uses ransack and tried it right away. But I can't seem to get it to work.
in my controller :
def search
#quotes = Quote.search(title_cont: q).result
#books = Book.search(title_cont: q).result
#users = User.search(username_cont: q).result
end
routes
get '/search', to: 'application#search'
view
<%= form_tag search_path, method: :get do %>
<%= f.label :title_cont %>
<%= f.search_field :title_cont %>
<%= text_field_tag :q, nil %>
<% end %>

You should use params[:q] instead of q. This should work
def search
#quotes = Quote.search(title_cont: params[:q]).result
#books = Book.search(title_cont: params[:q]).result
#users = User.search(username_cont: params[:q]).result
end
Also, f.label and f.search_field doesn't work with form_tag. You should use label_tag and search_field_tag instead
<%= form_tag search_path, method: :get do %>
<%= label_tag :title_cont %>
<%= search_field_tag :title_cont %>
<%= text_field_tag :q, nil %>
<% end %>

Why do you have two fields in your search form, you should only have one search field.
<%= form_tag search_path, method: :get do %>
<%= label_tag :title_cont %>
<%= search_field_tag :q %>
<% end %>

Related

Rails - passing multiple params to a form_tag

I have a form_tag that I'm using for a search. I need to pass not only the :term (what is being searched for), but also the :ques_num parameter. Currently, :ques_num is not being passed in.
<%= form_tag display_questions_path(:ques_num => 2), method: :get do %>
<%= text_field_tag :term, params[:term], placeholder: "Search Terms..." %>
<% end %>
This directs me to the address with the :term passed in, but not :ques_num. How would I go about doing this?
You should use hidden_field_tag instead:
<%= form_tag display_questions_path, method: :get do %>
<%= text_field_tag :term, params[:term], placeholder: "Search Terms..." %>
<%= hidden_field_tag :ques_num, 2 %>
<% end %>

How do i route a particular controller action to a particular form in rails?

Hi I am new to RoR and I was making a simple math_app. The addition function works fine. Now I'm trying to do a simple subtraction.The subtract controller is invoked and the subtract form is displayed but when I click on subtract the add controller gets invoked and an addition is performed. Where have I gone wrong?
This is my routes.rb:
Rails.application.routes.draw do
get 'subtract/form'
post 'subtract/result'
get 'add/form'
post 'add/result'
end
result.html.erb:
<%= #first %> - <%= #second %> = <%= #result %>
<br/>
<%= link_to 'back', subtract_form_path %>
form.html.erb: (This is the subtraction form)
<%= form_tag subtract_result_path do %>
<%= number_field_tag :first %>
-
<%= number_field_tag :second %>
<%= submit_tag "subtract" %>
<% end %>
subtract_controller.rb:
class SubtractController < ApplicationController
def form
end
def result
#first = params[:first].to_i
#second = params[:second].to_i
#result = #first - #second
end
end
You have problem in views/substract/form.html.erb. It now reads:
<%= form_tag add_result_path do %>
<%= number_field_tag :first %>
-
<%= number_field_tag :second %>
<%= submit_tag "subtract" %>
<% end %>
But it should be:
<%= form_tag substract_result_path do %>
<%= number_field_tag :first %>
-
<%= number_field_tag :second %>
<%= submit_tag "subtract" %>
<% end %>
You should probably also fix route
post '/subtract/result' => 'subtract#result', as: 'substract_result'
Also in views/substract/result.html.erb:
<%= #first %> - <%= #second %> = <%= #result %>
<br/>
<%= link_to 'back', subtract_form_path %>

Preserve State of Pushed Radio Buttons

How would one go about preserving the state of a "pushed" radio button after running a search? I'm using the Ransack gem to generate this search.
View:
<%= search_form_for #search do |f| %>
<strong>Location</strong></br>
<%= radio_button_tag("q[location_cont]", "Kentucky") %>
<%= label_tag "Kentucky" %></br></br>
<%= radio_button_tag("q[location_cont]", "Kansas") %>
<%= label_tag "Kansas" %></br></br>
<%= f.submit "Search" %>
<% end %>
Controller:
def index
#search = Job.search(params[:q])
#jobs = #search.result
end
If you were using form_tag, then radio_button_tag would be a valid field type to use. And you could use the third parameter to indicate if the button is selected or not:
<%= radio_button_tag("q[location_cont]", "Kentucky", params[:q] && params[:q][:location_cont] == "Kentucky") %>
<%= label_tag "Kentucky" %></br></br>
<%= radio_button_tag("q[location_cont]", "Kansas", params[:q] && params[:q][:location_cont] == "Kansas") %>
<%= label_tag "Kansas" %></br></br>
Since you are using the Ransack search_form_for, then you want to use radio_button and I think you'd want something like this:
<%= f.radio_button(:location_cont, "Kentucky") %>
<%= f.label :location_cont, "Kentucky", value: "Kentucky" %></br></br>
<%= f.radio_button(:location_cont, "Kansas") %>
<%= f.label :location_cont, "Kansas", value: "Kansas" %></br></br>

ruby on rails ignores my form_tag

I have written in my index.html.erb the following lines
<% form_tag illnesses_path, :method =>'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
expecting that it will write a form element but rails completrly neglected those line.
when I wrote
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
the items apeared (but no form element).
Why was the form element neglected
Change
<% form_tag illnesses_path, :method =>'get' do %>
to
<%= form_tag illnesses_path, :method =>'get' do %>.

link submit to another page

I have this form:
in header of my website:
<% form_tag request.path, :method => 'get' do %>
<p>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search User", :name => nil %>
</p>
<% end %>
controller:
def index
#title = "All users"
#users = User.paginate(:page => params[:page])
#users1 = User.simple_search(params[:query]).all
end
model:
acts_as_simply_searchable :columns => [:name, :email]
view
<%= will_paginate %>
<ul class="users">
<%= render #users1 %>
</ul>
<%= will_paginate %>
displays a user
I want to link the submit button to index.html.erb (i have assigned a path in routes.rb). So that the user can look at the search results
You don't do this at the submit button, but at the form_tag URL (the first parameter). It would be something like this:
<% form_tag users_path, :method => 'get' do %>
<p>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search User", :name => nil %>
</p>
<% end %>
Simply change request.path to the desired path.
I think you are missing the '=' sign and string interpolating the 'request.path'
This Worked for me:
<%= form_tag "#{request.path}", method: "get" %>
...the rest of your code ...
<% end %>

Resources