I have an edit form in erb.
<%= form_for #animal do |f| %>
Within the code I have a select with options:
<%= f.select :gender, options_for_select([['Mare'], ['Stallion'], ['Gelding']], :selected => :gender) %>
However, the select is not showing the correct selected value.
What could I be doing wrong?
I can get it to work if I hardcode it but of course that is not a viable option.
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'] %>
By the way, if you are using :include_blank => true, this will set your current selection to blank even though the form "knows" what is selected.
Related
I have a simple_form input field that looks like this:
<%= f.input :particular_users, collection: #all_users, input_html: { class: 'multiselectuser', multiple: true} %>
When I leave multiple: true off, the form submits the chosen value for the parameter :particular_users and I can see the value when debugging using "raise params.inspect". However when I leave the multiple: true option there, no vales get passed for the parameter :particular_users.
What am I doing wrong?
EDIT: I can not use the association input because :particular_users is a virtual attribute and has no relationship. I want the multiple select box to pass whatever values that are in there, even if they are arbitrary.
f.input :days, collection: #your_collection, input_html: { multiple: true }
It actually does work the way I wanted it to. The trick is to tell the strong parameters to allow a hash. It doesn't throw a strong parameters error, the param just gets thrown out and doesn't come through. So I set it to for example: params.require(:survey).permit(:particular_users => []).
To create multiple select tags with simple_form, use:
<%= f.association :particular_users, collection: #all_users, input_html: { class: 'multiselectuser'} %>
see part Associations in the gem description.
But as you don't want to use an ActiveRecord associaion, use select_tag:
<%= select_tag 'particular_users',
options_from_collection_for_select(#all_users, :id, :name),
multiple: true, class: 'multiselectuser' %>
I'm having trouble getting the selected value using the select_tag.
On my new.html.erb, I have a following code:
<%= select_tag "dams_provenance_collection[subjectType][]", options_for_select(subjectTypeArray), :prompt=>"Select subject type" %>
and on edit.html.erb, I want to use the selected value from new.html.erb as a default, so I tried:
<%= select_tag 'dams_provenance_collection[subjectType][]', options_for_select(subjectTypeArray, params[:selected]) %>
but it didn't work. Does anyone know how to set selected value from new.html.erb as default in edit.html.erb? Any help would be appreciated.
Try this,
<%= select_tag "dams_provenance_collection[subjectType][]", options_for_select(array_values, :selected => params[:option]) %>
Just try like this:
= select_tag :chart_time_id, options_for_select(#times_collect, :selected => params[:num_days].present? ? params[:num_days].to_i : 7), {class: 'form-control selectpicker', 'data-style'=> "btn-info", required: true}
Just check param present or not, and using inline operater (if -else)
I am loving the ransack gem for its flexibility, however I am unable to get the standard collection_select to function properly. Perhaps someone can assist.
Example:
<%= collection_select(:expense, :project_id, Project.all,
:id, :name, { prompt: 'Select Project'}, { class: 'span4' }) %>
in this case, this code is from an expense entry screen, so the first parameter is the expense object. What should it be in the ransack form? Also, I know I need to get the suffix in there. In this example, I would like project_id_eq to be the search pattern.
Also, my form is on a controller and view called "reports", I am not putting this search in the default controllers.
Thanks!
Seems that this will work.
<%= f.select :project_id_eq, options_from_collection_for_select(Project.all, "id", "name", #search.project_id_eq) %>
If anyone has another suggestion, would love to know it too.
To do this with an include_blank, put it outside of the parentheses:
ex:
<%= f.select :languages_id_eq, options_from_collection_for_select(Language.all, "id", "name"), include_blank: true %>
== UPDATE ==
better yet, use f.collection_select. This way after the user searches for something using the drop down, that option is preselected on the following page:
<%= form.collection_select :vendor_id_eq, Vendor.all, :id, :name, include_blank: true %>
I have a dropdown of prices using a select_tag, but each time I use the dropdown to set a price and then do the search then the values in the dropdowns go back to their default value when the search results are returned. How do I make the dropdown stay at the last value? Here's the code, I've got the :selected => at the end but don't know what value to give it:
<%= label :price, 'max', "to $" %>
<%= select_tag(:max, options_for_select([['$100,000', 100000], ['$200,000', 200000], ...['$20,000,000', 20000000]], :selected => ['$1,000,000', 1000000])) %>
:selected should point to the last element of the array element that you want to pre select.
Try
<%= select_tag(:max, options_for_select([['$100,000', 100000], ['$200,000', 200000], ...['$20,000,000', 20000000]], :selected => 1000000)) %>
If you want to keep the value from your current request you may go for
<%= select_tag(:max, options_for_select([['$100,000', 100000], ['$200,000', 200000], ...['$20,000,000', 20000000]], :selected => params[:max])) %>
See options_for_select on the Rails API as well.
I'm following this intro to rails: http://guides.rubyonrails.org/getting_started.html#creating-new-posts
and I want to be able to add data to my form thats not chosen in a field, that is just passed as a constant.
How do I do that?
<%= f.hidden_field :name, :value => value %>
See ActionView::Helpers::FormHelper