How to make the f.select rails selected - ruby-on-rails

My code:
<%= f.select :area, options_for_select([['a','a'],['b','b'],['c','c']]), {}, {:class => 'span3 controls controls-row'}, :selected => params[:area] %>
The result is:
ArgumentError in Users#edit
Showing /home/airson/rails_projects/friends_of_local/app/views/users/edit.html.erb where line #17 raised:
wrong number of arguments (5 for 4)
Why am I getting this error?

No need to use :selected just pass your params[:area] alone to options_for_select as a second argument:
<%= f.select :area,
options_for_select([['a','a'],['b','b'],['c','c']], params[:area]),
{}, { :class => 'span3 controls controls-row' } %>
The last value of your params[:area] will be selected.
Hope it helps ; )

You should pass :selected option to options_for_select method, like this:
<%= f.select :area, options_for_select([['a','a'],['b','b'],['c','c']], :selected => params[:area]), {}, { :class => 'span3 controls controls-row' } %>

The above answers didn't work for me on Rails 6.
This is what worked. Copied from one of the answers on Reddit
= f.select(:results, options_for_select(['Accepted', 'Not Accepted', 'Rejected', 'Acc', 'Rej', 'Information Only', 'N/A'], selected: #report.results || nil), { include_blank: "Select Result" }, { class: 'form-control select2', style: 'width: 100%;'})

Below is what worked for me using f.select with custom names and values including a blank field while using the Ransack search. Thanks to mayorsanmayor's example for getting me started in the right direction. This also works for rails 5.1.7.
<%= f.select(:status_eq, options_for_select([ ["Verified", "verified"], ["Non Verified","notVerified"]], selected: #q.status_eq || nil), {:include_blank => "Select Status"}, {class: 'form-control'}) %>

Related

Blank in drop-down list in options_from_collection_for_select

I'd like to include a blank in my drop-down list using options_from_collection_for_select but cannot seem to put in the usual collection parameter { :include_blank => true}. I cannot find a reference for this anywhere. Am I missing something? My statement is:
<%= select_tag "search", options_from_collection_for_select(TaskGroup.all.order("LOWER(task_group_name)"), "id", "task_group_name",params[:search]) %>
Thanking you in advance for your help,
Leah
Try this:
<%=
select_tag(
"search",
options_from_collection_for_select(
TaskGroup.all.order("LOWER(task_group_name)"), "id", "task_group_name",params[:search]
),
:include_blank => true
)
%>
Try this:
<%= select_tag "search", options_for_select(TaskGroup.all.order("LOWER(task_group_name)").map {|task_group| [task_group.task_group_name, task_group.id]}, params[:search]), {include_blank: "Select task group"} %>

Rails: Submit form on change

how can I get this to work?
<%= form_for current_user, html: {multipart: true } do |f| %>
<%= f.select(:brand, Brand.pluck(:company), {prompt:true}, {class: 'form-control'}, {:onchange => 'this.form.submit()'}) %>
<% end %>
The goal is to submit the form on change automatically. But the code above does not work. I get an wrong number of arguments (5 for 1..4) error. Any ideas?
The last three arguments there are actually a hash of options. Try putting them into curly braces to make it more clear, if you like:
<%= f.select(:brand, Brand.pluck(:company), {
prompt: true,
class: 'form-control',
onchange: 'this.form.submit()'
}) %>
Got it. Removed {prompt:true} and it works!
Select Helper:
select(object, method, choices = nil, options = {}, html_options = {}, &block)
This should work with prompt also:
<%= f.select(:brand, Brand.pluck(:company), {include_blank: true, class: 'form-control'}, :onchange => 'this.form.submit()') %>
Rails select helper

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 simple_form label for select

I'm trying to set a label for the following line but I keep getting an error if I add label_method anywhere. label: is just ignored. How can I add a label for f.select?
<%= f.select :state_identifier, Location::STATE, { prompt: 'State', id: 'state' } %>
I tried the following but it doesn't format properly in form-horizontal, leaving no gap between the label and data.
<%= f.label :state_identifier, label: 'State' %>
This is because f.select is not a simple_form method and does not support :label
Something like this should work for you w/ simple form.
<%= f.input :state_identifier, :label => "State", :collection => ["a","b"], :input_html => {:id=>"state" } %>
Hope this helps.

Using form_for multi-select fields with acts_as_taggable_on

I can't figure out the right code for using a predetermined set of options for a multi-select field. I want to have a list of skills in a drop down that users can select from. Here is the code I am using, it works fine as a single select field, but not as a multi-select:
<%= form_for(#user, :html => { :class => "form-stacked" } ) do |f| %>
...
<div class="clearfix"><%= f.select :skill_list, options_for_select(["Asst", "dir", "pres"]),
{
:multiple => true,
:class => "chzn-select",
:style => "width:450px;" } %></div>
...
<% end %>
Anyone have any suggestions? Eventually, I will want to store all of the options for the multi-select form elsewhere because there will be a bunch, but this is the first challenge I can't figure out..
Thanks.
EDIT
I have also tried:
:html => { :multiple => true, :class => "chzn-select", :style => "width:450px;" } and it doesnt work either
There needs to be two pairs of brackets, one for the options, and one for html_options, like so:
<%= f.select :skills_list, options_for_select(["Asst", "dir", "pres"]), {}, {:multiple => true, :class => "chzn-select", :style => "width:450px;" } %>
See the docs for the select helper.

Resources