I have a drop down list of Type in my Products model.
I want to be able to search in the Products index.html.erb so a user selects a type from a drop down list, clicks search and all the products matching that type are returned.
I can get normal search methods working where the user enters in their search in a text box but I cannot get it working when they just select from a dropdown.
Can anyone help?
In your controller :
def index
#products = Product.all :conditons => {:type => params[:type]}
end
In your view:
<% form_tag products_path, :method => :get do %>
<%=select_tag :type, options_for_select(Product::TYPES.map{ |type| [type, type]}), :onchange => "this.form.submit();" %>
<%=submit_tag "Search" %>
<% end %>
NB: The options_for_select accepts an array of pairs as [label, value], so we use map to build it.
Related
I have a search form and I need a checkbox that will select and then return whether a particular listing allows pets. I have created a custom route, controller method, and erb in the view. However, I am not accomplishing what I set out to do.
When a user clicks the Pets Allowed checkbox and then clicks search, the listings where pets allowed == true should be returned. I am not sure how to go about that.
This is the current code, but does not accomplish what I am after. This will redirect to /pets_allowed but that isn't a real thing.
listings_controller:
def pets_allowed
#listings = Listing.where(pets: true)
end
routes.rb:
get "pets_allowed" => "listings#pets_allowed"
html.erb:
<div>
<%= link_to 'Pets Allowed', pets_allowed_path, :class => 'button btn-transparent' %>
</div>
You probably need to use form_for instead of link_to.
<%= form_for :search_pets, url: pets_allowed_path, method: :get do |f| %>
<%= f.check_box :has_pets %>
<% end %>
Now in action,
def pets_allowed
#listings = Listing.where(pets: params[:search_pets][:has_pets])
end
When the checkbox is unchecked, it will return all the listings with no pets and when its checked it will return all the listings that have pets.
Hope that helps!
I have a hash called #records_hash which consists of 3 arrays: #records_hash["EVENT_DESCR"], #records_hash["EVENT_ID"], and #records_hash["PROCESSS_ID"]
I want the user to be able to select from a dropdown the event they would like to view and pass the corresponding values to the next page.
I want the user to see the EVENT_DESCR when they are using the dropdown, but I would like to pass the EVENT_ID and the PROCESS_ID to the next page.
Currently, I have a form that looks like this:
<%= form_tag({:controller => 'options', :action => 'sort', :type => 'event', :page => 1}, :method => 'get') do %>
<%= select_tag 'event_descr', options_for_select(#records_hash["EVENT_DESCR"]) %>
<%= submit_tag "Submit" %>
<% end %>
This form successfully lists out the EVENT_DESCR's in a dropdown format and sends the EVENT_DESCR to the next page.
How would I go about sending the EVENT_ID and the PROCESS_ID with it?
I think this is what you want:
# the values are whatever you want to pass. I'm just doing an array of arrays with EVENT_ID first.
<% values = #records_hash["EVENT_ID"].zip(#records_hash["PROCESSS_ID"]) %>
<% opts = options_for_select(#records_hash["EVENT_DESCR"].zip(values)) %>
<%= select_tag 'event_descr', opts %>
You can try out options_from_collection_for_select - you may need to modify your hashes to a collection of objects but this will make your code more readable. Probably a class XXEvent with id, descr and process_id as properties.
Ive got a select_tag field at the top of a page and trying to get the selected option to change the content on the page based on the users selection.
Im a learner and have found pieces of information around but without detailed examples and good explanations on how to best approach and implement.
The scenario is as follows:
I have a belongs_to association between a project and documents and in one of my views which lists documents, I want to only show all the documents that belong to the currently selected project in the select tag.
Passing the selected project's id to the documents index action which only shows documents for a specified project id via a link_to tag came to mind. This would thus refresh the page with the correct documents in the view but I believe that is not the correct way to do it and that I cant use link_to tags as options in a select_tag. Can anyone help and offer an example?
I would suggest using the form.select method and options_for_select as in
f.select :attribute, options_for_select(#array, default_value)
and in your controller you should create or update using the submitted parameter
n = record.new(:attribute => params[:attribute])
have fun
In your controller:
def index
if params[:project]
#documents = Document.where(:project => params[:project]
else
#projects = Project.all
end
end
In your form/view:
<%= form_tag 'projects', :method => :get do %>
<%= options_from_collection_for_select(#projects, :id, :name)
<%= submit_tag %>
<% end %>
<% if #documents %>
<%= #documents.each do |d| %>
....
<% end >
<% end %>
new on rails, i am having problem in passing select_tag value(in the view file) to controller.
my view controller file is like
class ProjectStatusController < ApplicationController
def index
#projects = Project.find(:all, :select => "name")
end
def show
lookup = params[:project]
#rows = Project.find_by_lookup(lookup)
end
end
and view file is like
<% form_tag("project_status", :controller => "ProjectStatus", :action => "show", :method=>'get' ) do %>
<%= select_tag 'project', options_from_collection_for_select(#projects,"id", "name"),:onchange => "this.form.submit();" %>
<% end %>
<%
if !#rows.nil?
#rows.each do |row|
end
%>
<%= row[:name] %>
<% end %>
what i basically want to achieve is this - based on the selected value from select tag
i want to display information(on the same view page) of selected item from the database
First of all you should write <%= form_tag not <% form_tag
Than, it's strange that your extract for select field id(options_from_collection_for_select(#projects,"id", "name"))
but in method show you search record by field lookup
You can use something of this sort to get this issue fixed
<%= check_box_tag "projectids[]",project.id %>
This would display the checkbox against each entry. And the checkbox is linked to the project object by its id. In the controller method, you will receive the project ids.
Then just directly use the submit tag to pass the parameters to the method.
I have a search field and button. When the user searches something like "cat" my view renders a bunch of related keywords such as "catnip", "cat toys", "cats" etc... each of these results are links, and are to pass the value of the string displayed back into the search to generate results for the selected link. For example:
User renders search page and searches for "cat"
view page renders results related to "cat" such as "catnip" "kittens"
User now clicks on "catnip"
View page renders results related to "catnip" such as "grass"
Is this possible with link_to? I'm lost and not quite sure what to do...
--
My Code:
SEARCH VIEW PAGE
<% form for(:search, url => search_path) do |f| %>
Search: <%= f.text_field :search %><br>
<%= f.submit "Search Keyword" %><p>
<% unless #keywords.nil? %>
<h3>Keyword Results</h3>
<% #keywords.each do |k| %>
<%= link_to k.name, :controller => "search", :action => "keywords", value => k.name %>
<% end %>
SEARCH CONTROLLER
def keywords
if request.post? then
#keywords = googlesearchapi(params[:search])
end
I want to pass the link_to value that the user clicks on as the :search parameter... thanks in advance for any advice~~
First of all, you want the link to be: "../search/keywords?search=catnip", to do this, modify the link_to as:
<%= link_to k.name, :controller => "search", :action => "keywords", :search => k.name %>
Then, you need to delete the line if request.post? then, otherwise it won't handle the requests coming from link_to, as it is a GET request (not POST).