Rails select options only seem to send 0 - ruby-on-rails

I am trying to use select options in rails
<%= f.select :category, options_for_select(["Select One", "Cell", "Work", "Office", "Home", "Other"],[1,2,3,4,5,6]) %>
Params
def story_params
params.require(:story).permit(:title, :body, :user_id, :category)
but the database table only seems to get "0" whenever my post is submitted, why is this?

I think you need to add like this instead
<%= f.select :category, options_for_select([["Select One", 1], ["Cell", 2], ["Work", 3], ["Office", 4], ["Home", 5], ["Other", 6]]) %>
Hope that helps!

options_for_select accept key and value in nested array format if you didn't pass value it will take key as value
so, you need to pass as key,value pair like [["Select One", 1],["Cell", 2]..etc]
For more reference view this

Related

undefined method `name' for ["Company Team Meeting", 25]:Array Rails options_for_select

I am working on the rails 5 application. I need to display dropdown of project.
I have #projects on controller and its values are
[["Company Team Meeting", 25], ["BuildEffective", 1], ["VCF ", 86], ["StomerijCollectief", 114], ["StomerijCollectief - Enhancement", 130], ["Stomerij Mobile App", 135], ["Blog Writing", 138], ["Stomerij Design Enhancements", 139]]
On view side I am using following code
<%= f.options_for_select(#projects.map {|p| [p.name, p.id]})%>
this gives me following error
undefined method `name' for ["Company Team Meeting", 25]:Array
Please help me
Please note: I am not in the projects controller
You can use <%= f.options_for_select(#projects, object.id) %>.
object_id will auto populate dropdown on edit.
Since you already have an array of pairs you can just pass it straight to options_for_select:
<%= options_for_select(#projects) %>
But if you have a collection of records instead (instead of two plucked columns or wherever this data comes from) with you can use the collection_select helper instead of manually building the option tags and select tag.
<%= f.collection_select(:project_id, Project.all, :id, :name) %>
# or
<%= f.collection_select(:project_ids, Project.all, :id, :name, multiple: true) %>
your #projects object is an Array type and so it don't have attributes like name and id.
you can place it like <%= f.select :projects, options_for_select(#projects) %>

Setting include_blank for an f.select with options_for_select

I have the following in my View:
<%= f.select :blood_group, options_for_select([
['O+', Membership.blood_groups.keys[0]], ['O-', Membership.blood_groups.keys[1]],
], #user.blood_group) %>
How do I add include_blank attribute to the above? When I just append it at the end, I get the error "wrong number of arguments (given 3, expected 1..2)"
You could do it this way:
<%= f.select :blood_group, [['', nil], ['one', 1], ['two', 2], ['three', 3], ['four', 4], ['five', 5], ['', nil]]
its optional to specify options_for_select note that this add two blanks field at the beginning and in the end.
You can do this following as:
<%= f.select :blood_group, options_for_select([
['O+', Membership.blood_groups.keys[0]],
['O-', Membership.blood_groups.keys[1]]], #user.blood_group, {include_blank: true}, {}) %>
You can refer document at: options_for_select

Rails: nested form binded to an object – problems with select box

I have a nested form model in my Rails app. When I go to edit an object in the model, all of my fields (text_area, check_box, etc.) are populated with data from the database except for the select box. How can I get my select box to also populate when I am using the form to edit existing data from the model?
<%= form_for #test do |f| %>
<%= f.fields_for :questions do |builder| %>
<%= render 'question_fields', f: builder %>
<% end %>
<% end %>
_question_fields.html.erb
<fieldset>
<%= f.text_area :question_text %>
<%= f.select :correct_answer, options_for_select([['A', 1], ['B', 2], ['C', 3], ['D', 4], ['E', 5]]), :prompt => 'Choose the correct answer' %>
<%= f.check_box :question_check %>
</fieldset>
You need to send the selected parameter in options_for_select function call.
<%= f.select :correct_answer, options_for_select([['A', 1], ['B', 2], ['C', 3], ['D', 4], ['E', 5]], :selected => f.object.correct_answer), :prompt => 'Choose the correct answer' %>
Try setting a value for selected.
Refer to: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select
Edit: Can't test it now but I'm pretty sure select will take the array of options by itself. So you shouldn't need options_for_select here - then selectshould set the selected option..
Try this and refer to: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select

Rails - drop down from an array of strings

I have an array like this:
['New York', 'Los Angeles']
And I want to be able to generate a select/option with those values in a form like this:
<%= form_tag filter_city_path, :method=> get do %>
<%= select_tag "city", #list_of_cities %>
<% end %>
But this is not working. As you can see I want to pass the selection as city in the url.
You need to use options_for_select helper like
<%= select_tag "city", options_for_select([['New York' ,'New york'], ['Los Angeles', 'Los Angeles']]) %>
My method for this is to build the array as a constant in the model, force confirmation to options listed in the constant, and call it from the view
class Show < ApplicationRecord
DAYS = [ "monday", "tuesday", "wednesday", "thursday","friday", "saturday","sunday"]
validates :day, inclusion: DAYS
end
If you want the option for that field to be submitted without content you'll have to call `allow_blank: true' in the validation as well. Once that is set up you can call the constant to populate the view in the form as so:
<%= select_tag "day", options_for_select(Show::DAYS) %>
or
<%= select_tag "day", options_for_select(Show::DAYS.sort) %>
if you want it presorted (which makes no sense with days of the week...)
It looks like your array doesn't have enough argument. Please refer to this guide.
The options typically needs to be formatted in this way:
[['Lisbon', 1], ['Madrid', 2], ...]
Take note the value 1, 2, etc.

Rails - map form values to database

How can I set up my Model so fields project_score, employee_score, and company_score all map their values to the database? This is what I mean:
'Low' maps to 1
'High' maps to 2
'Very High' maps to 3
'Urgent' maps to 4
Is there a way to get the textual output in my views, and separately convert that text back into the numbers before saving? Thanks.
I think what you're asking is as follows
<%= form_for #something do |f| %>
<%= f.select :project_score, [["Low", 1], ["High", 2], ["Very High", 3], ["Urgent", 4]] %>
<%= f.select :employee_score, [["Low", 1], ["High", 2], ["Very High", 3], ["Urgent", 4]] %>
<%= f.select :company_score, [["Low", 1], ["High", 2], ["Very High", 3], ["Urgent", 4]] %>
<% end %>
Alternatively, if you had your Scores in a database table with an id and name column.
<%= form_for #something do |f| %>
<%= f.select :project_score_id, Score.all.map{|s| [s.name, s.id]} %>
<% end %>
Where you would create an association to the score
belongs_to :project_score, :class_name => 'Score'
rather than use the integer. This would also mean you change your fields to project_score_id, employee_score_id, company_score_id
You could also create a custom FormBuilder that would create some form of score_select to make it easier down the road: http://code.alexreisner.com/articles/form-builders-in-rails.html.
When displaying, you'll also need some form of helper method to display the appropriate one:
module ScoreHelper
def score_display(score)
case score
when 1
"Low"
when 2
"High"
when 3
"Very High"
when 4
"Urgent"
end
end
end
# in your view
<%= score_display(#something.project_score) %>
Or if you have took the Score table and belongs_to association approach
<%= #something.project_score.name %>
You can set-up you model to return something like this:
RATINGS = [
["Low", 1],
["High", 2],
["Very High", 3],
["Urgent", 4]
]
Then <%= obj.select Model::FREQUENCY.each { |w| w } %> in the View.

Resources