質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

保存

保存(save)とは、特定のファイルを、ハードディスク等の外部記憶装置に記録する行為を指します。

Q&A

解決済

1回答

1894閲覧

【Rails】一括フォームの実装について

yastinbieber

総合スコア49

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

保存

保存(save)とは、特定のファイルを、ハードディスク等の外部記憶装置に記録する行為を指します。

0グッド

1クリップ

投稿2020/08/05 08:14

編集2020/08/09 07:15

前提・実現したいこと

Railsにて一括登録フォームの実装を試みています。

前回こちらにて同内容で質問させていただき実装できたと思ったのですが実際は保存がされておらず再度のご質問となります。

前回の質問内容

希望している実装としては、
newアクションより複数レコードを一回で登録させることです。

現状複数レコードを送信までできておりますがなにかしらの障壁により保存までがされていない状態です。

発生している問題・エラーメッセージ

error内容は下記のようにでています。

Parameters: {"utf8"=>"✓", "authenticity_token"=>"4jyslJtI/ALBv+pGEoSEb7ikIVHioo+6ZAkN4XkcEHEbAi/MeJr8RvS+/XDAh7nMJOHfrFGpiTDt8yI8kwq6KQ==", "form_advicediary_collection"=>{"advicediaries_attributes"=>{"0"=>{"advicemenu_id"=>"1", "weight"=>"100", "reps"=>"10", "weightsecond"=>"100", "repssecond"=>"10", "weightthird"=>"100", "repsthird"=>"10", "memo"=>"1", "user_id"=>"9", "idealweight_id"=>"4", "useradvice_id"=>"6"}, "1"=>{"advicemenu_id"=>"2", "weight"=>"100", "reps"=>"10", "weightsecond"=>"100", "repssecond"=>"10", "weightthird"=>"100", "repsthird"=>"10", "memo"=>"2", "user_id"=>"9", "idealweight_id"=>"4", "useradvice_id"=>"6"}, "2"=>{"advicemenu_id"=>"3", "weight"=>"50", "reps"=>"10", "weightsecond"=>"50", "repssecond"=>"10", "weightthird"=>"50", "repsthird"=>"10", "memo"=>"3", "user_id"=>"9", "idealweight_id"=>"4", "useradvice_id"=>"6"}, "3"=>{"advicemenu_id"=>"4", "weight"=>"0", "reps"=>"10", "weightsecond"=>"0", "repssecond"=>"10", "weightthird"=>"0", "repsthird"=>"10", "memo"=>"4", "user_id"=>"9", "idealweight_id"=>"4", "useradvice_id"=>"6"}}}, "commit"=>"今日のトレー ニングを完了する"} User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 9 ORDER BY `users`.`id` ASC LIMIT 1 ↳ /home/ec2-user/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.4.3/lib/active_record/log_subscriber.rb:98 Unpermitted parameters: :user_id, :idealweight_id, :useradvice_id Unpermitted parameters: :user_id, :idealweight_id, :useradvice_id Unpermitted parameters: :user_id, :idealweight_id, :useradvice_id Unpermitted parameters: :user_id, :idealweight_id, :useradvice_id (0.1ms) BEGIN ↳ app/models/form/advicediary_collection.rb:15 Advicemenu Load (0.2ms) SELECT `advicemenus`.* FROM `advicemenus` WHERE `advicemenus`.`id` = 1 LIMIT 1 ↳ app/models/form/advicediary_collection.rb:16 (0.2ms) ROLLBACK ↳ app/models/form/advicediary_collection.rb:15 Redirected to https://ae4965dfa4a04e8c87367d6e80606bec.vfs.cloud9.ap-northeast-1.amazonaws.com/advicediaries/choice Completed 302 Found in 104ms (ActiveRecord: 3.6ms)

該当のソースコード

##advicediaries_controller class AdvicediariesController < ApplicationController def new @form = Form::AdvicediaryCollection.new params[:group] @advicemenus = Advicemenu.where(group: params[:group]) end def create @form = Form::AdvicediaryCollection.new(advicediary_collection_params) binding.pry if @form.save redirect_to advicediaries_path else redirect_to choice_advicediaries_path end end private def advicediary_collection_params params .require(:form_advicediary_collection) .permit(advicediaries_attributes: Form::Advicediary::REGISTRABLE_ATTRIBUTES) end end
##models/advicediary.rb class Advicediary < ApplicationRecord validates :availability, inclusion: { in: [true, false] } validates :weight, presence: true validates :reps, presence: true validates :weightsecond, presence: true validates :repssecond, presence: true validates :weightthird, presence: true validates :repsthird, presence: true validates :memo, presence: true belongs_to :user validates :user_id, presence: true belongs_to :advicemenu validates :advicemenu_id, presence: true belongs_to :useradvice validates :useradvice_id, presence: true belongs_to :idealweight validates :idealweight_id, presence: true scope :search, -> (search_params) do unless search_params.blank? advicemenu_id_is(search_params[:advicemenu_id]) .date_from(search_params[:date_from]) .date_to(search_params[:date_to]) else Advicediary.where(created_at: Date.current.all_day) end end scope :date_from, -> (from) { where('? <= created_at', from) if from.present? } scope :date_to, -> (to) { where('created_at <= ?', to) if to.present? } scope :advicemenu_id_is, -> (advicemenu_id) { where(advicemenu_id: advicemenu_id) if advicemenu_id.present? } end
##models/form/advicediary.rb class Form::Advicediary < Advicediary REGISTRABLE_ATTRIBUTES = %i(advicemenu_id weight reps weightsecond repssecond weightthird repsthird memo) attr_accessor :advicemenu_id end
##models/form/advicediary_collection class Form::AdvicediaryCollection < Form::Base DEFAULT_ITEM_COUNT = 4 attr_accessor :advicediaries def initialize(attributes = {}) super attributes self.advicediaries = DEFAULT_ITEM_COUNT.times.map { Form::Advicediary.new } unless advicediaries.present? end def advicediaries_attributes=(attributes) self.advicediaries = attributes.map do |_, advicediary_attributes| Form::Advicediary.new(advicediary_attributes) end end def save Advicediary.transaction do self.advicediaries.map(&:save!) end return true rescue => e return false end end
##models/form/base.rb class Form::Base include ActiveModel::Model include ActiveModel::Callbacks include ActiveModel::Validations include ActiveModel::Validations::Callbacks end
##views/advicediaries/new.html.erb #フォーム箇所のみ抜粋 <%= form_for(@form, url: advicediaries_path, method: :post) do |fb| %> <%= fb.fields_for :advicediaries do |f| %> <!--<div class="row">--> <div class="form-group col-md-12 has-success"> <%= f.label :menu(メニュー) %> <%= f.collection_select :advicemenu_id, Advicemenu.all, :id, :menu, {}, class: 'form-control' %> </div> <div class="row"> <div class="form-group col-sm-6 col-md-2 col-lg-2 has-success"> <%= f.label :weight/重量(1セット目), class: "bmd-label-floating" %> <%= f.number_field :weight, placeholder: 'kg', class: 'form-control' %> </div> <div class="form-group col-sm-6 col-md-2 col-lg-2 has-success"> <%= f.label :reps/回数(1セット目), class: "bmd-label-floating" %> <%= f.number_field :reps, placeholder: 'reps', class: 'form-control' %> </div> <div class="form-group col-sm-6 col-md-2 col-lg-2 has-success"> <%= f.label :weight/重量(2セット目), class: "bmd-label-floating" %> <%= f.number_field :weightsecond, placeholder: 'kg', class: 'form-control' %> </div> <div class="form-group col-sm-6 col-md-2 col-lg-2 has-success"> <%= f.label :reps/回数(2セット目), class: "bmd-label-floating" %> <%= f.number_field :repssecond, placeholder: 'reps', class: 'form-control' %> </div> <div class="form-group col-sm-6 col-md-2 col-lg-2 has-success"> <%= f.label :weight/重量(3セット目), class: "bmd-label-floating" %> <%= f.number_field :weightthird, placeholder: 'kg', class: 'form-control' %> </div> <div class="form-group col-sm-6 col-md-2 col-lg-2 has-success"> <%= f.label :reps/回数(3セット目), class: "bmd-label-floating" %> <%= f.number_field :repsthird, placeholder: 'reps', class: 'form-control' %> </div> </div> <div class="form-group col-md-12 has-success"> <%= f.label :memo/メニューに対するメモがあればご活用ください, class: "bmd-label-floating" %> <%= f.text_field :memo, placeholder: '見返したときにわかるようにメモとしてご活用ください', class: 'form-control' %> </div> <%= f.hidden_field :user_id, value: current_user.id %> <%= f.hidden_field :idealweight_id, value: current_user.idealweight.id%> <%= f.hidden_field :useradvice_id, value: current_user.useradvice.id%> <% end %> <div class="row"> <div class="form-group col-md-3"> <br><%= fb.submit "今日のトレーニングを完了する", class: "btn btn-raised btn-success" %> </div> </div> <!--</div>--> <% end %> コード

試したこと

一括フォームの実装

【Rails 6】form_with用いて一括登録する

【Rails 5】モデルを一括登録する手順

今回一番上の「一括フォームの実装」をベースに実装を試みています。

補足情報(FW/ツールのバージョンなど)

Rails 5.2.4.3
ruvy 2.5.1

別途なにか必要な情報がありました仰ってください。
何卒宜しくお願いします。

追記

要は1つのフォームで同モデルのレコードを一括複数登録できれば良いです。
もし別途やり方等ございましたら教えていただけますと幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Unpermitted parameters: :user_id, :idealweight_id, :useradvice_id

user_ididealweight_iduseradvice_idの3つはUnpermitted parameters(許可されていないパラメータ)ですよ」と言われています。

Railsでstrong parametersという概念があります(もし分からなければ、調べてみて下さい)。
strong paramtersとして許可しているパラメータ以外を受け取ろうとすると、エラーになります。

private def advicediary_collection_params params .require(:form_advicediary_collection) .permit(advicediaries_attributes: Form::Advicediary::REGISTRABLE_ATTRIBUTES) end

のところで、コードをみた感じだと、permit()内のForm::Advicediary::REGISTRABLE_ATTRIBUTESで許可するパラメータを指定しています。

class Form::Advicediary < Advicediary REGISTRABLE_ATTRIBUTES = %i(advicemenu_id weight reps weightsecond repssecond weightthird repsthird memo) attr_accessor :advicemenu_id end

ここにエラー文で出てきた:user_id, :idealweight_id, :useradvice_idが定義されてないのが原因なのではないか、と推測しています。

見当違いだったら申し訳ないです;;

投稿2020/08/15 07:36

kumackey

総合スコア59

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yastinbieber

2020/08/15 08:24

ご回答ありがとうございます。 今試してみたところ実装することができました! とても困っていたので非常に助かりました!!本当にありがとうございました!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問