Rails f.select default selected - ruby-on-rails

I have select
<%= f.select :visibility, collection_for_visibility_select, :include_blank => false %>
And a helper with values for select:
def collection_for_visibility_select
[
[l(:label_crm_contacts_visibility_project), Contact::VISIBILITY_PROJECT],
[l(:label_crm_contacts_visibility_public), Contact::VISIBILITY_PUBLIC],
[l(:label_crm_contacts_visibility_private), Contact::VISIBILITY_PRIVATE]
]
end
I want to add default select value to the select, and this is what I tried:
<%= f.select :visibility, collection_for_visibility_select, :selected => Contact::VISIBILITY_PUBLIC, :include_blank => false %>
it gave me default select value, but when i want to edit record and switch visibility to something else,i still got VISIBILITY_PUBLIC
How do I fix it?

You can try this:
<%= f.select :visibility, collection_for_visibility_select, :selected => (f.object.visibility.nil? ? Contact::VISIBILITY_PUBLIC : f.object.visibility), :include_blank => false %>
It will read the value from the model first, and if it is nil, will use the default value.

Related

Add custom option to select value

I have a select with options filled in from a collection
<%= select('task', 'person_id', Person.where(:job_id => #job.id).order(:name).collect {|p| [p.name, p.id]}, {:include_blank => true}, :required => true) %>
I'd like to add a 'Not Applicable' option to this select but am unsure how. I have it set to add a blank and I also have it set to required. With both of those true, someone can't just choose the blank. I have the required set because I want my staff to think about the option they select.
Thanks for the help!
I'd create a helper method something like this
def person_options(options = {})
options_for_select([["Not Applicable", ""]] + Person.where(:job_id => #job.id).order(:name).collect {|p| [p.name, p.id]}, options)
end
Then you can call it from erb like so
<td><%= f.select :person_id, person_options(selected: #person. person_id, include_blank: true), {}, {style: 'width:auto'} %></td>
might need to tweak for your project

how to select all values by default of form_for select

In a form with form.select I want all options to be selected by default. I have age model, in form which is showing all ages I want to be selected all options by default. My code is following:
f.select(:age, options_for_select(Age.pluck(:age, :id) , :selected => #campaign.ages.pluck(:id)),{},{:multiple=>true , :required=>true})
Try this:
f.select(:age, options_for_select(Age.pluck(:age, :id) , :selected => #campaign.ages.pluck(:id)),{:multiple=>true , :required=>true})
I did this through checks. If a user is created as a new record, then I selected all of them and in case of edit, I will only show only selected. As code following speaks of itself:
<% if #campaign.new_record? %>
<%= f.select(:age, options_for_select(Age.pluck(:age, :id), :selected => Age.pluck(:id) ), {}, {:multiple => true,:required => true ,:class => 'form-control'}) %>
<% else %>
<%= f.select(:age, options_for_select(Age.pluck(:age, :id), :selected => #campaign.ages.pluck(:id) ), {}, {:multiple => true,:required => true ,:class => 'form-control'}) %>
<% end %>

How do I set a selected option in this Rails 2 selection form?

I am editing a Rails 2 application. In it, a user submits a form that includes a dropdown menu, and that information creates a record in my database. When the user goes to edit the record, I want to show the saved "selected" option in the dropdown menu. I keep getting errors, however. Here is my set-up:
View Select Field
<% form_for #newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
<%= f.select :start, options_for_select(#itinerary.locations), {:include_blank => true}, {:id=>"startdrop" } %>
Form Helper
def options_for_select(locations)
locations.map do |l|
content_tag "option", l.masterlocation.name, location_option_attributes(l)
end.join("\n")
end
private
def location_option_attributes(location)
{
:value => "#{location.masterlocation.street_address}, #{location.masterlocation.city}, #{location.masterlocation.state}, #{location.masterlocation.zip}",
:id => location.masterlocation.id,
:"data-masterlocation-name" => location.masterlocation.name,
:"data-masterlocation-id" => location.masterlocation.id,
:"data-masterlocation-latitude" => location.masterlocation.latitude,
:"data-masterlocation-longitude" => location.masterlocation.longitude
}
end
I have tried making the view look like this:
<%= f.select :start, options_for_select(#itinerary.locations, #newsavedmap.start), {:include_blank => true}, {:id=>"startdrop" } %>
But I get wrong number of arguments (2 for 1)) for that line. Also tried
<%= f.select :start, options_for_select(#itinerary.locations), {:selected => #newsavedmap.start, :include_blank => true}, {:id=>"startdrop" } %>
But nothing is preselected when I go to edit the saved map. I've tried following these links, and keep striking out. Appreciate any help you have to offer!
Rails select helper - Default selected value, how? , Rails form_for select tag with option selected , Rails select helper - Default selected value, how?
You could try something like this in your helper
def options_for_select(locations, selected=nil)
locations.map do |l|
tag_options = location_option_attributes(l)
if l == selected
tag_options[:selected] = "selected"
end
content_tag "option", l.masterlocation.name, tag_options
end.join("\n")
end
then call field like you were trying before.
<%= f.select :start, options_for_select(#itinerary.locations, #newsavedmap.start), {:include_blank => true}, {:id=>"startdrop" } %>
You can pass one more parameters to select the value like this:
<%= f.select :start, options_for_select(#itinerary.locations), :selected => #newsavedmap.start, {:include_blank => true}, {:id=>"startdrop" } %>

rails input selected values from params to select_tag (multiple => true)

I want to keep the select_tag(:multiple => true) options to be selected which were selected by user once search is performed
<%= select_tag 'values[]', method_for_options_for_select, :class => 'some-class', :multiple => true, :size => 6 %>
Suppose a user select 4 values from the select tag then for values should be selected,
How can we pass this 4 values to the select_tag?
I tried using :selected => params['values[]'] but this doesnt works for multiple true
Any help will be appreciated
Ref this and options_for_select
Something like following
<%= select_tag 'values[]',
options_for_select(#stores.map {|s| [s.store_name, s.store_id]},
#user.stores.map {|j| j.store_id}),
:class => 'some-class', :multiple => true, :size => 6 %>

Include blank for first item in select list in options_for_select tag

I tried :include_blank => true, but it didn't work.
<select>
<%= options_for_select Model.all.collect{|mt| [mt.name, mt.id]} %>
</select>
If I need to add it to the collection, how would you do that?
I think you want this format:
select("model_name", "model_id", Model.all.collect {|mt| [ mt.name, mt.id ] }, {:include_blank => 'name of your blank prompt'})
BTW: was assuming Modle was suppose to be Model. To use using collection_select:
collection_select(:model, :model_id, Model.all, :id, :name, :prompt => true)
I believe the :include_blank options only exist for select fields tied to a model.
Assuming you want to use a plain <select> tag instead of a <%= select(...) %> tied to a model, you can insert a blank entry at the front of your results:
<%= options_for_select Modle.all.collect{|mt| [mt.name, mt.id]}.insert(0, "") %>
Since you have tagged as select-tag you can use the option include_blank with select_tag.
From the documentation:
select_tag "people", options_from_collection_for_select(#people, "id", "name"), :include_blank => true
# => <select id="people" name="people"><option value=""></option><option value="1">David</option></select>
Or you can use options_for_select:
<%= select_tag column.name, options_for_select(Model.all.collect{|mt| [mt.name, mt.id]}), :include_blank => true %>
<%= options_for_select Model.all.collect{|x| [x.name,x.id]}.unshift(["",nil]) %>
= select_tag "some_value", options_for_select(Model.all.collect{ |x| [x.name, x.id]}.prepend([t('helpers.some_name'), nil]), :selected => params[:some_value])
If you want a slick solution you can use my gem rearmed_rails which has a feature in it that safely monkey patches options_for_select and options_for_collection_select
rails g rearmed_rails:setup
Open config/initializers/rearmed_rails.rb and change the following values to true
options_for_select_include_blank: true,
options_from_collection_for_select_include_blank: true
Now whenever you need a blank included simply do the following:
<%= options_for_select(Model.all.map{|x| [x.name,x.id]}, include_blank: true) %>
You can use the following monkey patch to add the include_blank argument to options_for_select
module OptionsForSelectIncludeBlankPatch
def options_for_select(container, selected = nil)
if selected.is_a?(Hash)
include_blank = selected[:include_blank] || selected['include_blank']
end
options = super
if include_blank
include_blank = '' if include_blank == true
if Rails::VERSION::MAJOR >= 5 && Rails::VERSION::MINOR >= 1
str = tag_builder.content_tag_string(:option, include_blank, {value: ''})
else
str = content_tag_string(:option, include_blank, {value: ''})
end
options.prepend(str)
end
options
end
end
ActiveSupport.on_load(:action_view) do
ActionView::Base.send(:include, RearmedRails::RailsHelpers)
end

Resources