前提・実現したいこと
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 %> コード
試したこと
今回一番上の「一括フォームの実装」をベースに実装を試みています。
補足情報(FW/ツールのバージョンなど)
Rails 5.2.4.3
ruvy 2.5.1
別途なにか必要な情報がありました仰ってください。
何卒宜しくお願いします。
追記
要は1つのフォームで同モデルのレコードを一括複数登録できれば良いです。
もし別途やり方等ございましたら教えていただけますと幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/15 08:24