accepts_nested_attributes_forを使って関連モデルを保存しようとしているのですが、コントローラで値を受け取れていないのか、データベースに値が入りません。
スケジュールの登録フォームで共有したい人をチェックボックスで選びScheduleとShareモデルを同時に保存しようとしています。
しかし、コントローラ側でshares_attributes: [:user_id]のuser_idが受け取れず困っています。
フォームから送られてきたuser_idを受け取るにはどのようにすればよいでしょうか?
ご教示よろしくお願いいたします。
Model
1class Schedule < ApplicationRecord 2 belongs_to :user 3 belongs_to :event, optional: true 4 has_many :shares, as: :shareable 5 accepts_nested_attributes_for :shares, allow_destroy: true 6 7class Share < ApplicationRecord 8 belongs_to :user 9 belongs_to :schedule 10 belongs_to :shareable, polymorphic: true 11
View
1<%= form_for(schedule) do |f| %> 2 <%= f.fields_for :shares do |share| %> 3 <%= share.collection_check_boxes(:user_id, @users, :id, :name, include_hidden: false) do |b| %> 4 <%= b.label { b.check_box + b.text } %> 5 <% end %> 6 <% end %> 7<%end>
Controller
1def schedule_params 2 params.require(:schedule).permit(:title, :body, :start_at, :end_at, :color, :allDay, :user_id, :branch_id, :team_id, :event_id, shares_attributes: [:user_id]) 3end
あなたの回答
tips
プレビュー