Strong Parametersの設定がうまくいかないのでご教授お願いいたします。
rails5.0.7において、ユーザーが予約をするときに他のテーブル(documentテーブル)に含まれるデータを選択して、予約データを保存したい。createアクションが正しく作動せず、保存ができず困っています。
発生している問題・エラーメッセージ
{"utf8"=>"✓", "authenticity_token"=>"iiHX/Md/K8KFpArwVOXsBPYCBWp5Bnda4Ezmr1CGslb1vuUZq6RzWs/FydLwGaisLXoDsrPd3RYTl/0XA2zAnA==", "reservation"=>{"receivedate"=>"20200226", "document_ids"=>["", "1"]}, "commit"=>"Save", "controller"=>"reservations", "action"=>"create", "user_id"=>"1"} permitted: false>
ビュー
.new-reservation =form_for [@user, @reservation] do |f| = f.text_field :receivedate, placeholder: "受け取り希望日を入力してください" = f.collection_check_boxes :document_ids, @documents, :id, :name = f.submit "Save"
モデル(reservation.rb)
class Reservation < ApplicationRecord belongs_to :user belongs_to :office has_many :documents validates :receivedate, presence: true end
モデル(document.rb)
class Document < ApplicationRecord has_many :office_documents has_many :offices, through: :office_documents belongs_to :reservation belongs_to :user end
ルーティング
Rails.application.routes.draw do devise_for :users root "reservations#index" resources :users, only: [:edit, :update] do resources :reservations, only: [:new, :create, :edit] end end
コントーラー
class ReservationsController < ApplicationController before_action :set_user, only:[:new, :create] def index end def new @reservation = Reservation.new @documents = Document.all end def create #binding.pry @reservation = Reservation.new(reservation_params) if @reservation.save redirect_to root_path, notice: '予約が完了しました' else render :index end end def edit end private def reservation_params params.require(:reservation).permit(:receivedate, document_ids: []).merge(user_id: current_user.id) end def set_user @user = User.find(params[:user_id]) end end
試したこと
・binding.pryでreservation_paramsの中身を確認したところ以下のようにストロングパラメータを設定したキーはtrueで返された。
{"receivedate"=>"20200226", "document_ids"=>["", "1"], "user_id"=>1} permitted: true>
・form_forの中身に以下を追加する前は正しく処理されていたので、正しく処理されていた時のビューとコントーラーに戻したが、正しく処理されなかった。その間にreservationモデルをrollbackして、office_idとreservation_idを追記してマイグレーションしたのが原因かと考えています。
= f.collection_check_boxes :document_ids, @documents, :id, :name
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。