I have this rails code :
<% status_a = [ ["DRAFT", "DRAFT"], ["OPEN", "OPEN"], ["CLOSE", "CLOSE"] ] %>
<%= form_for(:dash_action, url: brokers_dashboard_path ) do |f| %>
<%= f.select(:select_status, options_for_select(status_a), {}, selected:'OPEN' %>
<% end %>
When it runs, it generates this HTML code :
<select selected="selected" name="dash_action[select_status]" id="dash_action_select_status">
<option value="DRAFT">DRAFT</option>
<option value="OPEN">OPEN</option>
<option value="CLOSE">CLOSE</option>
...
But what I expect is :
selected="OPEN" and not "selected"
Why the select method is not doing what I want ?
Try following code snippet, default value should be the parameter of options_for_select
f.select :select_status, options_for_select(status_a, 'OPEN')
Related
How do I assign the selected option value:
<select class="select2-simple-dropdown">
<% Season.all.each do |season| %>
<option id="chosen-season" value="<%= season.id %>"><%= season.name %></option>
<% end %>
</select>
To a form's field, let's say: Voyage.given_season ?
Use the rails select field instead and do it like this
<%= f.select :season_id, Season.all.pluck(:name, :id), {},
{ class: 'select2-simple-dropdown'} %>
Hope this helps.
If you want your select input to accept multiple options, you can pass multiple: true
<%= f.select(:season_id, Season.all.collect {|m| [ m.name, m.id] }, class: "form-control select2-simple-dropdown", id: "list-markets", multiple: true) %>
https://aalvarez.me/posts/select2-with-simple-form-in-rails/
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.
I'm not sure why, but my form is not showing the options selected on submit, even though the params hash shows that the information is being returned to the page.
Collection select code:
<%= f.collection_select :post_topic_ids, PostTopic.all, :id, :name, {}, { multiple: true, class: 'form-control' } %>
Which renders:
<select multiple="multiple" class="form-control" name="post[post_topic_ids][]" id="post_post_topic_ids">
<option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option>
</select>
Params returned after form validation error
params = {"post"=>{"post_topic_ids"=>["", "1"]}}
Update
I have also tried:
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }), multiple: true %>
and:
<%= select_tag 'post_topic_ids', options_from_collection_for_select(PostTopic.all, "id", "name"), multiple: true %>
Which renders:
<select name="post_topic_ids[]" id="post_topic_ids" multiple="multiple"><option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option></select>
you need to specify which element is selected a third parameter
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }, --->selected_element<---), multiple: true %>
look at http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select for some examples.
I am using bellow code and function options_from_collection_for_select for generating options for user.
<%= select_tag 'receiver', options_from_collection_for_select(#user, 'id', 'email') %>
Above code generate bellow html:
<select id="receiver" name="receiver" style="display: none;">
<option value="1">email1#yahoo.com</option>
<option value="2">email2#gmail.com</option>
<option value="3">email3#gmail.com</option>
</select>
But i want email with username, e.g <optionvalue="1">email1#yahoo.com(some_user)</option>
Suggestion any alternate function or customize current function will be appreciated.
In your User model add a method :
def user_dispay_name
"#{email}(#{full_name})"
end
Now do :
<%= select_tag 'receiver', options_from_collection_for_select(#user, 'id', 'user_dispay_name') %>
But without options_from_collection_for_select this function i'm using below code:
<%= select_tag 'receiver', options_for_select(#user.map{ |c| ["#{c.email} (#{c.display_name})", "#{c.id}"] }) %>
I have an action that renders a view which contains this:
<select id ='dynamic_select'>
<option value = "<%= activity_path %>">All</option>
<option value = "<%= activity_path(:type => 'enrolled') %>">Enrolled</option>
<option value = "<%= activity_path(:type => 'redeem') %>">Redeem</option>
<option value = "<%= activity_path(:type => 'social') %>">Social</option>
</select>
What would be the correct/Rails way of rendering that select and mark as selected one of the options depending on the type parameter:
If there is no type parameter, select "All" options, if there is type=enrolled parameter select Enrolled option, and so on...
I have managed to do that client side with Javascript, but I am wondering what would be the Rails way of doing so.
Rendered HTML:
<select id="dynamic_select" name="dynamic_select">
<option value="/activity">All</option>
<option value="/activity?type=enrolled">Enrolled</option>
<option value="/activity?type=redeem">Redeem</option>
<option value="/activity?type=social">Social</option>
</select>
Something like the following. I have displayed the select_options here, but you should probably generate them in your controller and pass them through to the view.
The key is using options_for_select.
<% select_options = {"All" => activity_path} %>
<% %w{Enrolled Redeem Social}.each {|opt| select_options[opt] = activity_path(:type => opt.downcase)} %>
<% form_for(resource) do |f| %>
<%= s.select :dynamic, options_for_select(select_option, :selected => select_options[#default || "All"]) %>
<% end %>