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.
Related
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.
I am using friendly_id so that I can create such URLs:
/search/france/weekly/toyota-95
My routes:
scope to: 'search#show' do
get '/search/:car_country_id/:rental_type_id/:car_group_id', as: 'search_country'
end
At the search#show view I have a form:
<%= form_tag search_country_path, :method => :get do %>
<%= select_tag(:car_country_id, options_from_collection_for_select(CarCountry.all, :slug, proc {|c| c.name }, {:selected => #country}), class: "Select-control u-sizeFull") %>
<%= submit_tag t('shared.search'), class: 'btn btn-primary' %>
<% end %>
And search controller:
#country = CarCountry.friendly.find(params[:car_country_id])
So ok, my intention is to change the URL as:
/search/italy/weekly/toyota-95
But the thing is, Rails params always sending france as car_country_id when I select country from select tag and submit it.
So what should I do?
Currently, two car_country_id are sent to Rails server. You can rename one of them:
<%= select_tag(:new_car_country_id, options_from_collection_for_select(CarCountry.all, :slug, proc {|c| c.name }, {:selected => #country}), class: "Select-control u-sizeFull") %>
In your controller, you should check whether new_car_country_id exists. If it does, then redirect to the corresponding path.
Another way is to make sure that the two car_country_id are the same. You should change the form's submit path once select_tag is updated with JavaScript.
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.
I have a ruby on rails app that has a form. I was wondering if there is a way to make sure that a user has selected drop down menu items in both of the drop downs on this form before it is submitted and params are generated. Ideally I would like to throw and error warning them to pick 1 item on each of the drop downs and re-render the form.
The form is below:
<%= form_tag compare_products_path, method: :get do |f| %>
<%= select_tag :product_id1, options_from_collection_for_select(#products, 'id', 'name') %>
<%= select_tag :product_id2, options_from_collection_for_select(#products, 'id', 'name') %>
<%= f.submit %>
<% end %>
Please let me know how I can accomplish what I stated above.
SIDENOTE: I also implemented Select2 to make the form look nicer but could not find out of there is a quick validation trick in Select2 to accomplish what I said above, if there is a suggestion for that I can post the Select2 version,
Try this:
<%= select_tag :product_id1, options_from_collection_for_select(#products.where.not(:name=>nil), 'id', 'name'), :include_blank => "Please select...",:required=>true %>
<%= select_tag :product_id2, options_from_collection_for_select(#products.where.not(:name=>nil), 'id', 'name'), :include_blank => "Please select..." ,:required=>true %>
I'm having trouble getting this to work. What I want to do is have a dropdown that populates with team numbers, then depending on which one the user selects it takes them to the edit page for that team. I can get the dropdown to populate correctly, but I can not get it to take me to the correct page. When I test it a different way, it does take me to the correct page so I don't believe that it is a routing error.
The code with issues:
<%= form_tag edit_corevalue_path(#corevalue), method: 'get' do %>
<%= collection_select(:corevalue, :corevalue_id, Corevalue.all, :id, :teamNumber, :prompt => "Select Team") %>
<%= submit_tag "Submit!", :name => nil , class: "btn btn-large btn-primary" %>
<% end %>
I get the error:
No route matches {:action=>"edit", :controller=>"corevalues", :id=>nil}
But if I comment that out and test it with:
<%= link_to "TEST", edit_corevalue_path(2) %>
Then it works and takes me to the correct page. I believe the error is that it is not getting the correct id when using #corevalue, but I'm not quite sure how to fix it or what the proper syntax would be. Any help is greatly appreciated. Thank you.
I think you want to use :id instead of :corevalue_id as the 2nd argument to the collection_select function:
<%= collection_select(:corevalue, :id, Corevalue.all, :id, :teamNumber, :prompt => "Select Team") %>
See ActionView reference