##has_manyとbelongs_toを使って1つのフォーム内で2つのモデルに変更を加えようとするとエラーに
各種モデル
<Company(Model)>
has_many :evaluations accepts_nested_attributes_for :evaluations
<Evaluation(Model)>
belongs_to :company
###ビュー
<new.html.erb>
<%= form_for :company do |f| %> <div class="field"> <%= f.label(:com_name,"会社名") %> <%= f.text_field(:com_name) %><br> </div> <h3>データ入力</h3> <%= f.fields_for :evaluations do |b| %> <div class="field"> <%= b.label(:environment, "給料") %> <%= b.select :environment, options_for_select([["1", 1], ["2", 2],["3", 3], ["4", 4],["5", 5], ["6", 6],["7", 7], ["8", 8],["9", 9], ["10", 10]],), :include_blank => true %><br> </div> <% end %> <% end %>
コントローラー
<controller> ``` def create@company = Company.new(company_params) if @company.save format.html { redirect_to @company, notice: 'Company was successfully created.' } else format.html { render :new } end end private def company_params params.require(:company).permit(:com_name, :com_type, evaluations_attributes: [:environment, :salary, :overtime, :boss, :corporate_style, :management, :dangerous, :other]) end
##エラー内容 エラー箇所はcontroller内のCompany.newする時の**「company_params」** - **Unpermitted parameter: :evaluations** - **ArgumentError (When assigning attributes, you must pass a hash as an argument.):** ##質問 controllerでネストされた値を受け取れていない(はず)という認識です。 問題はevaluation_attributes: [ネストされている各種値]だと思います。 ただ、どの箇所の記述がおかしいのか見当がつかないため投稿しました。 どなたかお教えいただければ幸いです。(´-`)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/04 12:34
退会済みユーザー
2018/07/04 12:38
2018/07/04 12:45
退会済みユーザー
2018/07/04 12:51