データベースでenumを更新しようとしていますが、は正しくパラメータとして渡されないようです。Rails 5:selectからenumが更新に保存されない
:私はこれを持って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"}
/common/companies_controller.rb で:更新偶然にオン は、ハッシュ(3行目)の外のようです(列挙型として使用)「役割」を除いて、渡され、 VIEで
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
:私はこれを持って/models/company.rbで
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
私は、ドロップダウンから役割を選択するには、このラインを使用W:
<option value=<%= f.select :role, Company.roles.keys.to_a, class: 'form-control' %></option>
更新
これは、ビューがどのように見えるかです:
<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>
私は何を変更する必要があります"ロール"列挙型の更新/保存を行うには?しかし、それはの現在の値を示さなかった、更新でき <%= f.select :role, Company.roles.to_a.map { |w| [w[0].humanize, w[0]] }, {}, {class:"form-control m-b"} %>
他のソリューション:ソリューション
ため
アップデートこの編集アクションで、ドロップダウンがデータベースから実際の値を示したソリューションですアクションを編集します。
エラー何?モデルの検証結果を手掛かりにチェックしましたか? –
はあなたのパラメータが正しくないようです。役割は会社のハッシュの内側にあるべきです。あなたの意見は正しくありません。チェックフォーム 'select'。 –
エラーは発生しません。トランザクションはデータベースで更新されずにトランザクションをコミットした後、dashboard_pathにリダイレクトされます。モデルから列挙型の検証を削除しました。 – matiss