前提・実現したいこと
社内ToDoアプリを作成中です
user(社員)がboard(情報を載せる所)を作った後
そのboardを共有できる人を選択するのに
チェックボックスを使った、多対多の保存を行おうとしたのですが
下記エラーがでて進めなくなりました。
中間テーブルとして、roomを作っています。
発生している問題・エラーメッセージ
該当のソースコード
rooms_controller
def new @board = Board.find_by(id: params[:board_id]) @room = Room.new end def create @room = Room.new(room_params) if @room.save flash[:success] = 'チームができました。メンバーを選んでください。' redirect_to @room else flash[:danger] = 'チームの作成に失敗しました。' render :new end end private def room_params params.require(:room).permit(:board_id, { user_ids: [] } ) end
View/room/new
<h1>共有者の選択</h1> <%= form_with(model: @room, local: true) do |f| %> <%= f.hidden_field :board_id, :value => @board.id %> <%= f.collection_check_boxes :user_ids, User.all, :id, :name %> <%= f.submit '作 成', class: 'btn btn-primary btn-block' %> <% end %>
追加情報
user
class CreateUsers < ActiveRecord::Migration[5.2] def change create_table :users do |t| t.string :name t.string :email t.string :password_digest t.references :department, foreign_key: true t.boolean :admin, default: false t.timestamps end end end
class User < ApplicationRecord before_save { self.email.downcase! } validates :name, presence: true, length: { maximum: 10 } validates :email, presence: true, length: { maximum: 255 }, format: { with: /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i }, uniqueness: { case_sensitive: false } has_secure_password belongs_to :department has_many :boards, through: :rooms has_many :rooms has_many :logs, dependent: :destroy end
board
class CreateBoards < ActiveRecord::Migration[5.2] def change create_table :boards do |t| t.string :name, null: false t.references :user, foreign_key: true t.string :release t.timestamps end end end
class Board < ApplicationRecord has_many :users, through: :rooms has_many :rooms has_many :lists, dependent: :destroy end
room(中間テーブル)
class CreateRooms < ActiveRecord::Migration[5.2] def change create_table :rooms do |t| t.references :user, foreign_key: true t.references :board, foreign_key: true t.timestamps t.index [:user_id, :board_id], unique: true end end end
class Room < ApplicationRecord belongs_to :user belongs_to :board end
試したこと
user_idsの所でエラーが出ているようなので文字の書き間違いがないかなどは調べて回り
_idsのsかとも思ったのですが
view,controller共にsを消すと
別のエラーが出てしまいます。
補足情報(FW/ツールのバージョンなど)
開発環境
AWS9
Rails5.2.4.1
プログラミング歴
6ヶ月
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。