Saving Enum from Select in Rails 4.1

Saving enum from select in Rails 4.1

Alright, so apparently, you shouldn't send the integer value of the enum to be saved. You should send the text value of the enum.

I changed the input to be the following:

f.input :color, :as => :select, :collection => Wine.colors.keys.to_a

Which generated the following HTML:

<select id="wine_color" name="wine[color]">
<option value=""></option>
<option value="red">red</option>
<option value="white">white</option>
<option value="sparkling">sparkling</option>
</select>

Values went from "0" to "red" and now we're all set.


If you're using a regular ol' rails text_field it's:

f.select :color, Wine.colors.keys.to_a

If you want to have clean human-readable attributes you can also do:

f.select :color, Wine.colors.keys.map { |w| [w.humanize, w] }

Saving to an enum field in Rails 4.1.1

Instead:

<%= f.select :category, Product.categories, include_blank: "Select a category" %>

Try:

<%= f.select :category, Product.categories.keys, include_blank: "Select a category" %>

Explain:

In Product.categories hash {"t_shirt"=>0, "hoodie"=>1, "jacket"=>2} but in Product.categories.keys array what you need ["t_shirt", "hoodie", "jacket"] for select helper.

rails 4.1 enums not saving from view

I figured this out, although not entirely happy with the answer. The dropdown storing the enum values were uppercase. e.g. "Registration". When it tried to save it can't find "Registration", but it can find "registration". Saving enums with the correct case works just fine.

Anyway, I would have hoped I could use integers corresponding to the hash key, but that doesn't seem to work.

Edited

Another way to solve this would be ...

params.require(:stream).permit(:stream_type, :course_presentation_id, :is_active, :created_by_user_id, :updated_by_user_id).tap do |w|
w[:stream_type] = w[:stream_type].to_i if w[:stream_type]
end

And Another post I found

Alternative solution

Rails 5: enum from select not saved on update

Parameters: {"utf8"=>"✓", "authenticity_token"=>"5mjLbYZH6tJWOympfEApF2EwbR/Nk4cQFvqtCiXhL3hOIs+yOQGbZUaIFqO42aMyUWr3JdX+dYQU4Arkpy3jrQ==", 
"company"=>{"name"=>"Agency 8", "legal_name"=>"agency No8 SIA", "reg_number"=>"123456789", "address"=>"street 8", "bank_acc"=>"123456789", "description"=>"Super agency",
"website"=>"www.google.com"}, "role"=>"seller_buyer", "commit"=>"Save", "id"=>"13"}

Look at the "role"=>"seller_buyer" it is out side of company hash. It should be like this:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"5mjLbYZH6tJWOympfEApF2EwbR/Nk4cQFvqtCiXhL3hOIs+yOQGbZUaIFqO42aMyUWr3JdX+dYQU4Arkpy3jrQ==", 
"company"=>{"name"=>"Agency 8", "legal_name"=>"agency No8 SIA", "reg_number"=>"123456789", "address"=>"street 8", "bank_acc"=>"123456789", "description"=>"Super agency",
"website"=>"www.google.com", "role"=>"seller_buyer"}, "commit"=>"Save", "id"=>"13"}

it is happening because your form input is not correctly generated. You should not be use select inside option. select will generate options by the given array(Company.roles.keys.to_a).

Edit:
This should fix your design issue:

<%= f.select :role, options_for_select(Company.roles.keys.to_a, class: 'form-control'),{},{class:"form-control m-b"} %>

Select enum from form to set role

To start, enum is not the name of an attribute. The name of the attribute is role.

Take a look at the rails-devise-pundit example application, specifically the file app/views/users/_user.html.erb which is a partial that creates a form to allow the administrator to change a user's role. I doubt you want to use a collection_select for helper (that is suitable if you have a separate Role model). Instead, an ordinary select form helper will work.

Here's a simple example that hardcodes the role options:

<%= f.select(:role, [['User', 'user'], ['Vip', 'vip'], ['Admin', 'admin']]) %>

Here is a better example that avoids hardcoding the roles in the form:

<%= f.select(:role, User.roles.keys.map {|role| [role.titleize,role]}) %>

The statement obtains an array of roles from the User model and constructs an array of key-value pairs using the map method.

How to create select for enum attribute in Rails 4 with simple Form

You can do it like that :

f.input : os, :collection => Employee.statuses.keys.to_a

You need to use the plurialized version of status.

Error displaying select field from enum on model

Few examples before the answer

# Assume life_cycle was set to 'annual'
puts p.life_cycle
#=> "annual"
p.life_cycle = 0 # set life_cycle as 0
puts p.life_cycle
#=> "annual"
p.life_cycle = 2 # we set to biennial
puts p.life_cycle
#=> "biennial"

You could also do this to see if life_cycle is annual by p.annual? results in true or false

And when you do this, using a plural of the enum, you can access it on class as a class method.

puts Plant.life_cycles
{"annual" => 0, "perennial" => 1, "biennial" => 2}

So yeah as @razr describes you would use the class method to get the hash and extract keys to form your select menu which is Plant.life_cycles.keys

In Rails 4.1, how to find records by enum symbol?

ActiveRecord::Enum provides scopes based on its values.

Just try:

Conversation.active

or

Conversation.archived

Of course, you can create your own scopes as Kyle Decot mentioned.



Related Topics



Leave a reply



Submit