Ruby on Rails - search results to show up on the current page - ruby-on-rails

I have a search bar available on every page of the webapp by adding
<body>
<%= form_tag(class_data_index_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search your major" %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<%= yield %>
in application.html.erb.
When I search, it shows the results, but it also navigates to /class_data?search=SearchKeyword. I want the results to show up on the current page, whether it'd be /home, /anothermodel.
How can I achieve this?

You want to use a remote form with <%= form_tag remote: true %> - see Rails form_tag remote example as an example and a starting point.

Related

Rails 5: Two search form_tags on the same page

I'm trying to implement a two search form_tag on a the same page, each search form is placed inside dynamic bootstrap tabs. The first one which is working is basic a search form with one field. The second one which is not working has two fields, one is the same search method as the first and the other I'm trying to get the address from the other_location field and via params[:other_location].
With the current setup the other_location field form the second form does not appear!
Both of the forms are inside partials and I am rendering them inside two dynamic bootstrap tabs like this:
<%= render 'pages/search' %>
<%= render 'pages/search_other' %>
<%= form_tag search_items_path, :method => "get" do %>
<%= text_field_tag :search, params[:search], autofocus: true,
class: "search-query search_size",
placeholder: "Enter product to search" %>
<%= submit_tag "Search", name: nil, :style => "display: none;" %>
<%end%>
<%= form_for :search_other_path, :method => "get" do |form| %>
<%= form.text_field :search, autofocus: true,
class: "search-query search_size",
placeholder: "Enter keyword to search" %>
<% form.fields_for :other_location_path, :method => "get" do |f| %>
<%= f.text_field :other_location, class: "search-query search_size",
placeholder: "Enter address to search" %>
<%= form.submit "Search", name: nil, :style => "display: none;" %>
<%end%>
<%end%>
model
def self.search(search)
return where("0=1") if search !~ /\w{4}/
where("lower(title) LIKE lower(:term)", term: "%#{search}%")
end
routes.rb
get 'search' => 'pages#search', as: 'search_posts'
get 'search' => 'pages#search_other', as: 'search_other'
get 'search' => 'pages#other_location', as: 'other_location'
controller:
def search_other
if params[:search]
#posts = Post.near(other_location,10).search(params[:search]).page(params[:page])
else
#posts = []
end
end
def other_location
other_location = params[:other_location]
if params[:other_location]
Geocoder.search(params[:other_location])
end
end
def search
if params[:search]
#posts = Post.near(action,10).search(params[:search]).page(params[:page])
else
#posts = []
end
end
On your route file:
get 'search/other' => 'pages#search_other', as: 'search_other'
get 'search' => 'pages#search_other', as: 'search_other_items'
both GET requests are going to your pages_controller.rb #search_other method. So even if you have the two form_tags sending the data to different paths (search_other_path, and search_other_items_path) it would be going to the same controler method - which is redundant.
On your actual HTML you have two form tags:
<%= form_tag search_items_path, :method => "get" do %>
and
<%= form_tag search_other_items_path, :method => "get" do %>
You have not mentioned search_items_path in your routes, so I have no idea where that's pointing to. Likely its a proper controller that works since you mentioned the first form was the only one working.
Now, your mentioned controller only has a search method. So to start you are looking at the wrong controller. You should be looking at the controller methods being referenced by the form's action.
In this case, the second form is sending it's request to search_other_items_path which according to your routes, its pointing to pages_controller.rb -> #search_other method.
You should edit your question to include code that is actually relevant. Maybe then I can actually help.

Redirect search form

I've followed this tuto to make a search form in my app.
It works well but it doesn't do exactly what I want
At the moment, I've to go to /search to see my result but I want that when a search is made, everywhere in my app, it's redirect on /search and the results are displayed.
My search controller :
def search
if params[:q].nil?
#idees = []
else
#idees = Idee.search params[:q]
end
end
My search form view
<%= form_for search_path, method: :get do |f| %>
<p>
<%= f.label "Search for" %>
<%= text_field_tag :q, params[:q] %>
<%= submit_tag "Go", name: nil %>
</p>
<% end %>
And my route :
get '/search', to: 'search#search'
It has to be a problem with my route but I don't know how to do this
Update your form as:
<%= form_for :search, url: search_path, method: :get do |f| %>
You may need to update you search action too to support nested params.

how to link form_tag to button rails 4

I have a selection box on my page, and when I click the submit button I want to take the selection choice to the server as either a post or get variable (I don't think it matters). How do I link this form:
<%= form_tag(store_rates_path, method: 'get') %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
to this button:
<%= button_tag(link_to("Get Rates", store_rates_path))%>
You only need to provide the path to the form_for method, to link it to the rates action of your stores controller:
<%= form_tag(store_rates_path, method: "get") do %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select((1980..2014).to_a)) %>
<%= button_tag "Get Rates" %>
<% end %>
In your rates action you can then retrieve the :year parameter passed as follows:
def rates
#year = params[:year]
end
You also need to define the route in your routes.rb file as follows, if you haven't yet:
get 'stores/rate', to: 'stores#rate', as: 'store_rates'
IMPORTANT
Just note that if the rates belong to a specific store, meaning the url is something like stores/1/rate then the above get must be stores/:id/rate, which also means you need to pass the store.id to the store_rates_path in your form: store_rates_path(#store)
You can use rails submit_tag helper
<%= form_tag(store_rates_path, method: 'get') %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
<%= submit_tag "Get Rates" %>
<% end %>
OR
If you want to use a link or button to submit your form parameters then you can use some js magic to achieve it:
<%= form_tag store_rates_path, id: "store-form", method: 'get' %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
<%= link_to "Get Rates", "#", id: "store-form-btn" %>
<% end %>
$(document).on("click","#store-form-btn",function(e){
e.preventDefault();
$("#store-form").submit();
});

Is it possible to replace a button/submit within a form_for or simple_form_for with a glyphicon/raw html?

I've been toying with using icons instead of standard buttons. I like the look of it and occasionally a icon is clearer than a labelled button.
While I found it easy to implement for link_to calls;
<%= link_to raw('<i class="icon-exclamation-sign"></i>'), '#' %>
I am struggling to achieve the same result when nested with form_for (or simple_form_for). Is there a way to use an icon for a submit or input when working with the form?
Here are some example forms from my code using the standard buttons:
<%= simple_form_for :present, url: present_path(list_item), method: 'put' do |f| %>
<%= f.hidden_field :purchased, value: "1" %>
<%= f.submit "owned" %>
<% end %>
<%= form_for :present, action: 'new', url: presents_path do |f| %>
<%= f.hidden_field :purchased, value: '0' %>
<%= f.submit "List", class: "btn btn-mini" %>
<% end %>
I've tried a few methods but none of yielded just the icon acting as a button - most render the button with either the raw HTML as its contents or the icon within the button.
The end result I'd like to be able to implement is just the icon acting as a click-able item (similar to the link_to result).
you probably want to look at image_submit_tag http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-image_submit_tag

Rails3 replace search box with select dropdown

Is there a way to replace a text_field_tag with a select in a rails3 search function?
My current search form:
<%= form_tag orders_path, :method => 'get', :id => "orders_search" do %>
<p><%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %></p>
I wanted to replace with a dropdown to limit / filter my customers results. I've tried this:
<%= select :search, params[:search], ([["Pending"], ["Open"], ["Closed"]]) %>
However this gives me a 500 error and an output in the log:
TypeError (expected Array (got String) for param `search'):
I've also tried:
<%= select :search, ([["Pending"], ["Open"], ["Closed]]) %>
Which leads to an invalid number of arguments.
What's the best way to do this?
Check Select helper methods in Ruby on Rails out.
You can probably do what you need with:
<%= select_tag "search", options_for_select([ "Pending", "Open", "Closed" ], params[:search]) %>
-- EDIT --
In order to submit form on select add following in your application.js (provided you use jQuery):
$(document).ready(function() {
$("select#search").change(function(){
$(this).closest("form").submit();
});
});
There are already few stack overflow answers in regards to this.

Resources