Rails drop down box posting null - ruby-on-rails

I have the following drop down box in a form for rails:
<%= f.select (:boolean, options_for_select([["Yes", 1], ["No", 0]])) %>
Other drop down boxes in the same form post correctly, but this one posts null. Others in the same form:
<%= f.select (:kids_in_college, %w{1 2 3 4 5 6 7 8}) %> #posts correctly
<%= f.select (:year, %w{2009-2010 2010-2011 2011-2012}) %> # posts correctly
Is there something wrong with my syntax?

I think problem is not with the select list.
I think you have to handle it on controller side.
Something like following
#obj.boolean = (params[:obj][:boolean]=="1")? true : false
Note:- As per my knowledge most likely problem will be you are trying to input "String" in the field where boolean is expected.

Related

collection_select (Select) in Rails not changing values on databse

So I have the collection_select working fine, however i have :include_blank option, and I want when the include_blank value is selected, to delete the ID from the database and leave it null.
How can I achieve this?
I was able to achieve this with the link mentioned below
<%= f.select(:user_id, [['Team', 0]] + User.all.collect { |p| [p.email, p.id]}) %>

Rails - Select tag not pre-selected after Form's validation fail

I have select tag
<%= f.select :gender, ["M", "F"], include_blank: "---" %>
If other field has error, the select tag will be reset to "---" even though I have picked an option before.
The solution that I found is using collection_select which takes a ActiveRecord Model to be the option-value pair.
Is there other way to do this?
Thanks
[EDIT]
The answer from #Micah is correct but since my form is Nested, it's different syntax.
Here's my code:
<%= f.fields_for :users do |ff| %>
...
<%= ff.select :gender, ["M", "F"], include_blank: "---",
selected: ff.object.gender ? ff.object.gender : "" %>
...
<% end %>
When I handle errors I usually use render to display the previous page again so I'm going to explain how to handle that first.
When you render a view the params that got passed into your previous command carry over, meaning you'll have params[:object][:gender]as a parameter when you return to the view. Thus
<%= f.select :gender, ["M", "F"], include_blank: "---", selected: params[:object] ? params[:object][:gender] :"" %>
will set the selected value back to what was selected the first time through. Basically what it's doing is taking a conditional and then providing an if/else in the same line. (conditional) ? if true do this : else do this. In this case the conditional is that params[:object] exists (meaning you've attempted to create/edit once before) and if so reassign value otherwise leave blank and select default value, your "---".
If you use redirect_to you'll actually have to manually pass the params back through the redirect but that's fairly simple and just takes the following:
redirect_to new_object_path(params)

Rails select_tag value Does Not Save in Database

I have a form in rails using form_for where users are able to enter in information about an event. One of the options is a dropdown list for ages allowed. A
All of the other information on the form is being sent to the database however this value is never recorded.
The column datatype for :age is integer.
Here is how I'm currently implementing it.
<%= select_tag(:age, options_for_select([['All Ages', 1] , ['18+', 2] ,['21+', 3]])) %>
I can provide more information if needed.
Your select tag seems to be disconnected from your model. Try select instead:
<%= f.select :age, options_for_select([['All Ages', 1] , ['18+', 2] ,['21+', 3]]) %>

How can I allow NULL values in a drop down list using Ruby on Rails?

Suppose I had a dropdown list in my form that allows users to select their favorite hot drink.
<%= form_for #person do |f| %>
<%= f.select :hot_drink, [['Tea', 'tea'], ['Coffee', 'Coffee']]
...
I want users to able to enter a NULL value if they don't like hot drinks. Something like this:
<%= f.select :hot_drink, [['Nothing Selected', NULL], ['Tea', 'tea'], ['Coffee', 'coffee']]
I know about the :include_blank option, but it's not what I'm looking for because it inserts an empty string into the database, which is not the same as a NULL. There are many NULL values already in my database, and I use this form for inserting and editing the Person's records. I need the dropdown list to recognize NULLs so I can edit the other fields in Person without being forced to change the value of hot_drink.
Anyone know how I could go about this?
Have you tried
<%= f.select :hot_drink, [['Nothing Selected', nil], ['Tea', 'tea'], ['Coffee', 'coffee']] %>
Try to add a default value to nil in your migration to handle this case.

Array as Parameter from Rails Select Helper

I'm working on a legacy project that is using acts_as_taggable_on which expects tags to come in arrays. I have a select box allowing users to select a tag on a Course in a field called categories. The only way mass assignment create will work is if params looks like this params = {:course => {:categories => ['Presentation']}}. I've currently a view with this helper:
<%= f.select 'categories', ['Presentation' , 'Round Table' , 'Demo', 'Hands-on'] %>
Which will give me a parameter like params = {:course => {:categories => 'Presentation'}}. This doesn't work since Acts as tag gable apparently can't handle being passed anything other than a collection.
I've tried changing categories to categories[] but then I get this error:
undefined method `categories[]' for #<Course:0x007f9d95c5b810>
Does anyone know the correct way to format my select tag to return an array to the controller? I'm using Rails 3.2.3
I didn't work with acts_as_taggable_on, but maybe this simple hack will be suitable for you? You should put it before mass-assignment.
category = params[:course][:categories]
params[:course][:categories] = [category]
If you are only going to allow the selection of ONE tag, you could do:
<%= f.select 'categories', [['Presentation'] , ['Round Table'] , ['Demo'], ['Hands-on']] %>
Each one item array will have first for the display value, and last for the return value, which in this case will both return the same thing, as the first element of the array is the same as the last element when the array as one element.
Seems like select doesn't give you that option.
If I understand correctly, one option might be to use a select_tag instead and just be explicit about where you want the selection in the params:
<%= select_tag 'course[categories][]', options_for_select(['Presentation' , 'Round Table' , 'Demo', 'Hands-on']) %>
That ought to get your params the way you need them.
Here's what I'm using for one of my projects:
<% options = { include_blank: true } %>
<% html_options = { required: true, name: "#{f.object_name}[#{resource.id}][days][]" } %>
<%= f.select :days, DAYS, options, html_options %>
Without html_options[:name], Rails handles the name of the select tag and spits out something like
service[service_add_ons_attributes][11][days]
but I need
service[service_add_ons_attributes][11][days][]
So I override it.
Hope that helps.

Resources