Passing HTML name value into rails select field - ruby-on-rails

I have a form that has a select field where the user selects their title. I want to pass a name value into the rails code so the field can be modified with a client-side validation framework. The client-side validations validate each field based on the name of the field.
My current solution isn't passing the name value of 'title' into the final HTML rendered in the browser.
The field
<%= f.select :title, options_for_select([["Title", "0"], ["Mr.", "Mr."], ["Mrs.", "Mrs."], ["Ms.", "Ms."], ["Dr.", "Dr."], ["Prof.", "Prof."]], selected: "0", disabled: ["0"]), :name => "title" %>
The current HTML output
name="user[title]"

<% possible_options = [["Title", "0"], ["Mr.", "Mr."],
["Mrs.", "Mrs."], ["Ms.", "Ms."], ["Dr.", "Dr."], ["Prof.", "Prof."]]
%>
<%= f.select :title, options_for_select(possible_options), {}, {:name => "title"} %>
It's better to prepare this possible_optins list in controller action itself & accessing here just like an instance variable.
Just for a quick fix, let it be in this view/form.

Related

Rails form_for selectbox with defined values view value from db

I have such form partial for CRUD actions:
= form_for #vehicle do |f|
= f.select :fuel_type, [["Выбрать", "None"], "Бензин", "Дизель", "Газ", "Гибрид", "Электричество", "Другое"], {:required => true, :selected => #vehicle.fuel_type}
So as you can see in db i store hole word, as for example Бензин or Дизель etc...
But when i try to edit using my form partial some data, i see that as default selected value of my selectbox is None (displayed as Выбрать). But when in db that field is Бензин why it is set to None in form view? How to see Бензин?
Also note that for new action #vehicle.fuel_type will be empty
I would pair the labels with a value, and store the value in the database as an integer:
[['Бензин', 1], ['Дизель', 2], ['Газ', 3]]
You can include blank on your collection to select none:
f.select :fuel_type, [['Бензин', 1], ['Дизель', 2], ['Газ', 3], ...], { :include_blank => "Выбрать" }
In this way you won't need :selected => #vehicle.fuel_type on your edit form as the value will be populated from the model if selected.
There is probably a better way to handle it and this functionality could also be done in the model, but to display the saved value you could create a helper method:
def map_fuel_type(value)
case value
when 1
'Бензин'
when 2
'Дизель'
when 3
'Газ'
when 4
'Гибрид'
when 5
'Электричество'
when 6
'Другое'
end
end
And then to show the value on your index or show page:
<%= map_fuel_type #vehicle.fuel_type %>
To store as string:
f.select :fuel_type, ["Бензин", "Дизель", "Газ", "Гибрид", "Электричество", "Другое"], { :include_blank => "Выбрать" }

How do I set the default value in my form on page load? (Rails)

I'm trying to set the default value of my form field, but I'm not sure where I can pass in that information. Here's what I have so far:
def month_options
[["JAN", "01"], ["FEB", "02"], ["MAR", "03"], ["APR", "04"], ["MAY", "05"], ["JUN", "06"], ["JUL", "07"], ["AUG", "08"], ["SEP", "09"], ["OCT", "10"], ["NOV", "11"], ["DEC", "12"]]
end
In my view, I'm using this:
<%= select_tag :month, options_for_select(month_options, params[:month]) %>
I'd like to be able to set the default value of my form field to the current month.
Perhaps
<%= select_tag :month, options_for_select(month_options, params[:month] || "JAN") %>
...if you want the selected value to default to "Jan" of params[:month] is nil.
Pass the selected option like this:
:selected => params[:default_value]
You can pass a parameter, string, etc whatever your case may be.

How to preselect dropdown given info from database to select_tag?

I have this line and after it is submitted as a form, C7000 or C3000 gets stored as a string. For the edit page, I cannot get the stored value to be the preloaded option on the dropdown instead of "Select One". What am I doing wrong?
= select_tag :enclosure_model, "<option>Select One</option><option>C7000</option><option>C3000</option>".html_safe, :disabled => false
Thanks
<%= select_tag :enclosure_model, helper.options_for_select([["select one", ""], ["c700"], "c700"], "c700"), :disabled => false %>
The above code will select c700

need to add default value in f.select field to existing ones - rails 3.2

With the code I have below in the select field I have all the public_campaigns:
<%= f.select :campaign_id, #public_campaigns.map{|x| [x.name,x.id]} %>
public_campaigns is defined in controller with:
#public_campaigns = #logged_in_user.campaigns.order('created_at desc')
In the form I select the campaign and fill up the rest of the form and at the submit action an invitation is created with campaign_id taken from the campaign I selected in the form, it can be anything from 1 to n
What I need now is to have a default item in select field that will have the value of 0 and named "No campaign", it means I invite someone to a campaign that I have not created yet and in invitation the campaign_id field will be 0.
Thank you for your time.
Do you really need 0? I think use of {:include_blank => "No campaign"} should be enough?
Try this:
<%= f.select :campaign_id, (#public_campaigns.map{|x| [x.name,x.id]} << ["No campaign",0]), {:selected => 0} %>
Well, the fastest way you can do this is something like this:
#public_campaigns = #logged_in_user.campaigns.order('created_at desc')
no_campaign = Campaign.new(:id => '0', :name => 'No Campaign')
#public_campaigns.unshift(no_campaign)
I'm not sure why you are unable to do it this way:
<%= f.collection_select :campaign_id, #public_campaigns, :id, :name, prompt: 'No campaign' %>
Just check if campaign_id.nil? instead of assigning any value to campaign_id

Rails select tag not working when value different from display or when value in database is integer?

In my rails form I'm using the select tag.
<%= f.select :featured,
Timeline::TIMELINE_FEATURED,
:prompt => "Select" %>
The definition of TIMELINE_FEATURES is Yes and No to be displayed, but stored as INT in database:
TIMELINE_FEATURED = [
# Displayed stored in db
["No", "0"],
["Yes", "1"]
]
My problem is that when I go to the edit page the dropdown value is NOT selected correctly.
For example, if I had set the dropdown to Yes, and store the value of 1 in the database if I come back to this dropdown Yes is NOT selected like it should be. Is it because the value is stored as an Integer in DB but it's a string in the option tag? How do I solve?
I have other selects in the same form that are working correctly but the value stored in the DB is a string.
Thank you in advance.
<%= f.select :featured, options_for_select(Timeline::TIMELINE_FEATURED, #zero_or_one), :prompt => "Select" %>

Resources