スケジュール管理アプリで予定の共有機能を作ろうとしています。
collection_check_boxesを使ってユーザ一覧を表示させ、チェックを入れたユーザに対して共有する仕組みにしたいのですが、collection_check_boxesでチェックを入れた値がコントローラ側で受け取れず、ご教示いただければ幸いです。
Schedule
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, reject_if: :all_blank 6end
Share
1class Share < ApplicationRecord 2 belongs_to :user 3 belongs_to :schedule, optional: true 4 belongs_to :shareable, polymorphic: true 5 6 validates :shareable_id, presence: true 7 validates :shareable_type, presence: true 8 validates :user_id, presence: true 9end
ScheduleController
1def new 2 @schedule = Schedule.new 3 @schedule.shares.build 4end 5 6def schedule_params 7 params.require(:schedule).permit(:title, :body, :start_at, :end_at, shares_attributes: []) 8end
form
1<%= form_for(schedule) do |f| %> 2〜省略 3 <%= f.fields_for :shares do |share| %> 4 <%= share.collection_check_boxes(:user_id, @users, :id, :name, include_hidden: false) do |b| %> 5 <%= b.label { b.check_box + b.text } %> 6 <% end %> 7 <% end %> 8<%end%>
html
1<label for="schedule_shares_attributes_0_user_id_15"><input type="checkbox" value="15" name="schedule[shares_attributes][0][user_id][]" id="schedule_shares_attributes_0_user_id_15" />日本 太郎</label>
あなたの回答
tips
プレビュー