Rails Erb Form Helper Options_For_Select :Selected

rails erb form helper options_for_select :selected

In your code, your options_for_select() call sets the selected value to "gender" and does not attempt to use the value from your form object.

Please see the docs for options_for_select() for usage examples.

options_for_select(['Mare', 'Stallion', 'Gelding'], f.object.gender)
options_for_select(['Mare', 'Stallion', 'Gelding'], :selected => f.object.gender)

Alternatively, you can do this, which will already use the gender() value for your form object:

<%= f.select :gender, ['Mare', 'Stallion', 'Gelding'] %>

options_for_select selected value not working

The documentation states:

(...) 
options_for_select(container, selected = nil) public
(...)
(million examples)

So you shouldn't pass selected: something, but just something. Also, this something needs to be selected value, not text:

= f.select :user_id, options_for_select(@users.pluck(:name, :id), @projectuser.id), include_blank: true

Rails options_for_select with selected model value

I can remember myself dealing with those issues. IMHO in those cases the approach I followed was to meet my expectations with similar "generators" and combinations of them.

There is another post that might help you.

Also do not forget to check out the proper syntax (I am talking about order).

Then, try using "selected: " to see how you go.

Hope it helps,

How to tell options_for_select field where to save selected option value?

You need to give the select tag a name (e.g. <select name="foo"> populates params[:foo], <select name="foo[bar]"> populates params[:foo][:bar]; alternatively use the select_tag/select form helper methods – see https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-select_tag and https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-select).

Be aware: If you haven't used Rails for several years, chances are you don't know about strong parameters which you need to use if you want to do direct assignment (e.g. User.new(params[:user]) doesn't work as it used to in older Rails versions – you need to use strong parameters here). Details: https://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters.

Can't get options_for_select to display choices

That is definitely possible. You were close, but you need to format your form helper like this to get the desired result:

<%= f.select(:state_id, options_for_select(State.all.collect {|p| [ p.home_state, p.id ]), :label => "State", :class => "dropdown-menu")%>

More docs here: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select

ruby on rails f.select options with custom attributes

Rails CAN add custom attributes to select options, using the existing options_for_select helper. You almost had it right in the code in your question. Using html5 data-attributes:

<%= f.select :country_id, options_for_select(
@countries.map{ |c| [c.name, c.id, {'data-currency_code'=>c.currency_code}] }) %>

Adding an initial selection:

<%= f.select :country_id, options_for_select(
@countries.map{ |c| [c.name, c.id, {'data-currency_code'=>c.currency_code}] },
selected_key = f.object.country_id) %>

If you need grouped options, you can use the grouped_options_for_select helper, like this (if @continents is an array of continent objects, each having a countries method):

<%= f.select :country_id, grouped_options_for_select(
@continents.map{ |group| [group.name, group.countries.
map{ |c| [c.name, c.id, {'data-currency_code'=>c.currency_code}] } ] },
selected_key = f.object.country_id) %>

Credit should go to paul @ pogodan who posted about finding this not in the docs, but by reading the rails source. https://web.archive.org/web/20130128223827/http://www.pogodan.com/blog/2011/02/24/custom-html-attributes-in-options-for-select

How to use the option selected in options_for_select in ruby on rails


<%= form_tag forecast_report_pdf_path do %>
<%= select_tag(:report_id, options_for_select([["Summary1", 7], ["Summary2", 6]])) %>
<%= hidden_field_tag :format, :pdf %>
<%= submit_tag("Generate Report") %>
<% end %>

This code worked. Thanks much for your inputs!

Convert a options_for_select to simple_form that uses two collections

I determined that i needed to change the method and give the model input all the available models, and let the javascript handle the filtering.

This works for me now.

 <div class ="form-group clearfix">
<%= f.input :listing_known_model, label: 'Model',
collection: vehicle_selection.all_model_choices,
input_html: {
class: 'vehicle-model form-control select2 jcf-ignore',
id: 'photo-ad-order-form-listing-known-model',
data: {
placeholder: 'Any Model'
}
}
%>
</div>

vehicle_selection.rb

def model_choices
@model_choices ||= make.present? ? model_source.call(make) : []
end

def all_model_choices
@all_model_choices ||= ->{ MakeModel.models }
end

How to have a drop down select field in a rails form?

You can take a look at the Rails documentation . Anyways , in your form :

  <%= f.collection_select :provider_id, Provider.order(:name),:id,:name, include_blank: true %>

As you can guess , you should predefine email-providers in another model -Provider , to have where to select them from .



Related Topics



Leave a reply



Submit