matiss matiss - 1 month ago 14
Ruby Question

Rails 5: enum from select not saved on update

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?

Answer
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).