Display selected option when editing a form (Rails 4) - ruby-on-rails

I want to display my dropdown form selected option value whenever an user edits the form. Right now it displays the first option (in this case a blank option).
Form
<%= f.select(:condition, options_for_select([["Brand New", "Brand New"], ["Pre-owned", "Pre-owned"]]), :include_blank => true, :selected => params[:condition]) %>
HTML Output
<select id="product_condition" name="product[condition]">
<option value=""></option>
<option value="Brand New">Brand New</option>
<option value="Pre-owned">Pre-owned</option>
</select>
JSON
{
id: 2,
condition: "Pre-owned",
}
Thanks.

<%= f.select(:condition, options_for_select([["Brand New", "Brand New"], ["Pre-owned", "Pre-owned"]], :selected => f.object.condition), :include_blank => true) %>

Check the options_for_select documentation, and you will discover that the last parameter is the selected option.
options_for_select(container, selected = nil)
In your case
<%= f.select(:condition, options_for_select([["Brand New", "Brand New"], ["Pre-owned", "Pre-owned"]], params[:condition]), :include_blank => true) %>
assuming params[:condition] contains the currently selected value, and the value matches the corresponding value in the select tag.
In other words, for "Pre-owned" to be selected, params[:condition] must contain "Pre-owned".

<%= f.select :condition, options_for_select([["Brand New", "Brand New"], ["Pre-owned", "Pre-owned"]], f.object.condition), {:include_blank => true} %>

<%= form.label :Hobbies%>
<%= form.select(:hobbies, options_for_select(%w[cricket football hockey
reading], :selected => form.object.hobbies )) %><br><br>
This form multiple drop down but user only select one and if you edit and show method show only one record.

Related

How to pass selected value in select tag through form_tag rails

I need to pass selected option values in rails form tag.Below code I am using.When I click Add to User group button,it will call addToUserGroups action.I need to get selected dropdown value when I click this button.
<%= form_tag(contoller: "custom_group", action: "addToUserGroups") do%>
<%=submit_tag "Add to User Group", class: "btn mx-auto btn-primary", style: "width: 200px"%>
<% end %>
I need to send this selected dropdown value to addToUserGroups controller.How to achieve this?
<select id="select-update" class="form-control">
<% #all_execgroup_list.each do |parameter| %>
<option value="<%= parameter['groupname'] %>"><%= parameter['groupname']%></option>
<% end %>
</select>
You can use the "select" option which is the second argument in the `options_for_select' helper.
<%= f.select :color, options_for_select(#color_options, selected: #color_value) %>
The important part here is selected: #color_value
Verbose version:
<%= f.select :color, options_for_select([["Blue", "blue"],["Green","Green"],["Yellow","yellow"]], selected: "yellow") %>
Yellow will be the default selected value in the dropdown.
options_for_select first argument takes a 2d array. What gets displayed in the dropdown is in first element in the array and the value is the second element in the array.
[["Display Name", "value"],["Display Name 2", "value 2"]]
You can use select_tag for that, like below
<%= select_tag :field_name_here, options_for_select(all_execgroup_list.all) %>

How to dispaly selected value in edit page

How to dispaly selected value in edit page.
in my form i need to display selected value. it is not selecting the value which i selected and submitted. else it shows "please select Study material".
This is my StudyMaterial model
class StudyMaterial < ActiveRecord::Base
TYPES = ['Question Paper', 'Book', 'Audio', 'Video']
enum study_material_type: TYPES
end
This is my 'form.html.erb'
<select class=" required form-control" name="study_material[study_material_type]" id="study_material_study_material_type" data-validation="required" data-validation-error-msg="Select study material">
<option value="">Please select study material</option>
<option value="Question Paper">Question Paper</option>
<option value="Book">Book</option>
<option value="Audio">Audio</option>
<option value="Video">Video</option>
</select>
How to dispaly selected value in edit page.
I am getting this error when i click edit studymaterial page
Please help me to solve this error
<%= form_for #study_material do |f| %>
<%= f.select :study_material_type, StudyMaterial::TYPES.map{|v| [v,v]}, selected: f.object.try(:study_material_type) , required: true, include_blank: "Select" %>
<% end %>
I think, you look something like that:-
<%= form_for #study_material do |f| %>
<%= f.select :study_material_type, StudyMaterial::TYPES, include_blank: "Please select study material", required: true %>
<% end %>
It will display selected value.

Add value to select in rails

i have a select that take values from database.
<%= select_tag :location, options_from_collection_for_select(Country.all, :id, :country_name), { :class => 'selectpicker' } %>
So i get all countries from database.
How i can do for add a custom value(for example Any, with value 0), to this select list taken from database ?
For example now i have:
<select>
<option value="UK">UK</option>
</select>
and i want get this:
<select>
<option value="0">Any</option>
<option value="UK">UK</option>
</select>
Thanks.
You can use the either prompt or include_blank options (FormOptionsHelper) as follows:
<%= select_tag :location, options_from_collection_for_select(Country.all, :id, :country_name), :prompt => 'Any', :class => 'selectpicker' %>

Adding blank option to rails select tag

Im using jquery chained, and Im trying to get the second drop down list to gray out if the first has a blank option selected. Im assuming I need a blank option in the second list for it to lock out, but I'm not sure how to add a blank option. Here is the select option
<%= select_tag :equipment, options_for_select(Equipment.all.collect
{ |e| ["#{e.model} - #{e.serialNum}",e.id,
:class =>"#{e.handReceipt}"]},
html_options = {:id=>'equipment'}) %>
The first drop down list lets you select the hand receipt type, and with jquery chained, the second list only shows records with the appropriate hand receipt attribute.
How would I add a blank option to the above select?
Edit- Here is what I've tried so far -
<%= select_tag :equipment,
options_for_select( [["--",""],
Equipment.all.collect{ |e|
["#{e.model} - #{e.serialNum}",
e.id, :class =>"#{e.handReceipt}"]}],
html_options = {:id=>'equipment'}) %>
This results in an improper display of the list-
<select id="equipment" name="equipment">
<option value="">--</option>
<option value="["M4 - W432156", 10, {:class=>"Arms Room"}]">["PSN-13 - 176985", 1, {:class=>"Commo"}]</option>
</select>
Instead of showing all the records in the table, it just shows a blank option and the second option.
<%= select_tag :equipment,
options_for_select( :include_blank => true,
Equipment.all.collect{ |e|
["#{e.model} - #{e.serialNum}",
e.id, :class =>"#{e.handReceipt}"]},
html_options = {:id=>'equipment'}) %>
Results in the following error -
C:/Users/Sam/Documents/ruby/btrp/app/views/vehicles/edit.html.erb:19: syntax error, unexpected ',', expecting tASSOC
e.id, :class =>"#{e.handReceipt}"]},
Your parameters at options_for_select is wrong, I think that's the right way:
<%= select_tag :equipment,
options_for_select(Equipment.all.collect { |e|
["#{e.model} - #{e.serialNum}", e.id,
{ :class =>"#{e.handReceipt}" }]}),
:include_blank => true,
:id => 'equipment' %>
Read more at:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
You can append the blank value like this.
<%= select_tag "category","<option value=''>Category</option>" + options_from_collection_for_select(#store_categories, "id", "name",params[:category].to_i)%>

Keep selected value in collection_select as active after submit

Dear All,
View
<form align="center" name="gm" action="">
<label for="col1"><b>Name: </b></label>
<%= collection_select(#table, "gm", #pop1, "col1", "col1", :prompt => true) %>
<%= submit_tag value="Proceed-->"%>
<form name="sp" action="">
<label for="col2"><b>Class: </b></label>
<%= collection_select(#table, "sp", #pop2, "col2", "col2", :prompt => true) %><br><br>
<%= submit_tag value="Submit"%>
</form>
Here, these are relational collection_select. I need to populate the second collection_select once first collection_select was selected. But, once "Proceed" submit was processed, params[gm] was disabled. So I am unable to process "Submit" tag with both params[gm] and params[sp] for #table. Any idea to keep selected value in collection_select to remain after click "Proceed".
If I use
<%= collection_select(#table, "gm", #pop1, "col1", "col2", :prompt => true, :selected=> params[:gm]) %>
It works!

Resources