Reputation: 747
I'm trying to update/save enum in database, however seems it is not passing as param correctly. On Update paramters are passed, except "role" (used as enum) which seems to be outside of hash (3rd line):
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"}
In /common/companies_controller.rb I have this:
def edit
@company = Company.find(params[:id])
end
def update
@company = Company.find(params[:id])
if @company.update_attributes(company_params)
flash[:success] = "Company updated"
redirect_to dashboard_path
else
#render 'edit'
flash[:error] = "Nothing happened!"
end
end
private
def company_params
params.require(:company).permit(:name, :legal_name, :reg_number,
:address, :bank_acc, :currency, :description, :website, :role)
end
In /models/company.rb I have this:
class Company < ApplicationRecord
#Companies has many users
has_many :accounts, dependent: :destroy
has_many :users, through: :accounts
has_many :user_roles, through: :users, source: :roles
enum currency: {eur: 1}, _suffix: true
enum role: {seller_buyer: 1, seller: 2, buyer: 3}, _suffix: true
accepts_nested_attributes_for :accounts, :users
In view I use this line to select role from drop-down:
<option value=<%= f.select :role, Company.roles.keys.to_a, class: 'form-control' %></option>
Update
This is how view looks like:
<div class="middle-box text-center loginscreen animated fadeInDown">
<%= form_for([:common, @company]) do |f| %>
<form class="m-t" role="form" action="">
<div class="form-group">
<%= f.text_field :name, class: 'form-control', placeholder: "Name"%>
<%= f.text_field :legal_name, class: 'form-control', placeholder: "Legal name"%>
<%= f.text_field :reg_number, class: 'form-control', placeholder: "Registration number"%>
<%= f.text_field :address, class: 'form-control', placeholder: "Address"%>
<%= f.text_field :bank_acc, class: 'form-control', placeholder: "Bank account"%>
<%= f.text_field :description, class: 'form-control', placeholder: "Description"%>
<%= f.text_field :website, class: 'form-control', placeholder: "Website"%>
<select class="form-control m-b" name="role">
<option value=<%= f.select :role, Company.roles.keys.to_a, class: 'form-control' %></option>
<%= f.submit "Save", class: "btn btn-primary block full-width m-b" %>
<% end %>
</div>
</form>
What should I change to make "role" enum update/save?
Update for solution
This is solution where in Edit action, drop-down shows actual value from database:
<%= f.select :role, Company.roles.to_a.map { |w| [w[0].humanize, w[0]] }, {}, {class:"form-control m-b"} %>
Other solutions allowed to update, however it did not show current value in Edit action.
Upvotes: 0
Views: 1731
Reputation: 9351
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"} %>
Upvotes: 2
Reputation: 3633
This looks really weird to me.
<option value=<%= f.select :role, Company.roles.keys.to_a, class: 'form-control' %></option>
You can generate drop-down by simply writing this. You have to add class to the f.select to get the style you need.
<%= f.select :role, Company.roles.keys.to_a, {}, class: 'form-control m-b' %>
Upvotes: 1