前提・実現したいこと
rails初心者です
多対多のアソシエーションにおいてcollection_check_boxを実装したいです
発生している問題・エラーメッセージ
collection_check_boxesを実装しようと記述しviewを表示するとエラーが発生します
view/rooms/new.html.erb
ruby
1<div class='hotel-post-contents'> 2 <div class='hotel-left-contents'> 3 <div class='hotel-post-main'> 4 <h2 class='hotel-post-title'>宿泊施設の登録を始めましょう</h2> 5 <%= form_with model:[@hotel, @room], local: true do |f| %> 6 <%# ホテル名と説明%> 7 <div class="new-hotel"> 8 <div class="weight-bold-text"> 9 部屋名 10 </div> 11 <%= f.text_field :room_name, class:"hotel-name-text", id:"room-name", placeholder:"部屋の名前", maxlength:"40" %> 12 <div class="hotel-information"> 13 <div class="weight-bold-text"> 14 定員人数 15 </div> 16 <%= f.text_field :capacity, class:"hotel-text", id:"capacity", placeholder:"例)6", maxlength:"2" %> 17 <div class="weight-bold-text"> 18 料金 19 </div> 20 <%= f.text_field :price, class:"hotel-text", id:"price", placeholder:"例)3000" %> 21 <div class="weight-bold-text"> 22 部屋トップ画像 23 </div> 24 <%= f.file_field :image, id:"room-image" %> 25 <div class="weight-bold-text"> 26 設備 27 </div> 28 <%# 設備は今後実装%> 29 <div class='facility-contents'> 30 31 <%= f.collection_check_boxes :facility_ids, Facility.all, :id, :name, include_hidden: false do |b| %> 32 <% b.label {b.check_box + b.text} %> 33 <% end %> 34 35 36 </div> 37 38 <div class='registration-button'> 39 <%= f.submit "登録" ,class:"registration-blue-button" %> 40 </div> 41 </div> 42 <% end %> 43 </div> 44 </div> 45 <div class='hotel-right-contents'> 46 </div> 47</div>
該当のソースコード
rooms_controller.rb
ruby
1class RoomsController < ApplicationController 2 3 4 def new 5 @hotel = Hotel.find(params[:hotel_id]) 6 @room = Room.new 7 end 8 9 def create 10 @hotel = Hotel.find(params[:hotel_id]) 11 @room = Room.new(room_params) 12 13 if @room.save 14 redirect_to hotel_path 15 else 16 render :new 17 end 18 end 19 20 private 21 def room_params 22 params.require(:room).permit(:room_name, :capacity, :price, :image, {facility_ids: []}).merge(owner_id: current_owner.id, hotel_id: params[:hotel_id]) 23 end 24end
routes.rb
ruby
1root to: 'reservations#index' 2 resources :hotels, only: [:index, :new, :create, :show] do 3 resources :rooms, only: [:new, :create] 4 end
room.rb
ruby
1class Room < ApplicationRecord 2 belongs_to :owner 3 belongs_to :hotel 4 has_many :reservations 5 has_many :room_haves 6 has_many :facilities, through: :room_haves 7 has_one_attached :image 8end
facility.rb
ruby
1class Facility < ApplicationRecord 2 has_many :room_haves 3 has_many :rooms, through: :room_haves 4end
room_have.rb
ruby
1class RoomHave < ApplicationRecord 2 belongs_to :room 3 belongs_to :facility 4end
rooms migration
ruby
1class CreateRooms < ActiveRecord::Migration[6.0] 2 def change 3 create_table :rooms do |t| 4 t.string :room_name, null: false 5 t.string :capacity, null: false 6 t.string :price, null: false 7 t.references :owner, foreign_key: true 8 t.references :hotel, foreign_key: true 9 t.timestamps 10 end 11 end 12end
facility migration
ruby
1class CreateFacilities < ActiveRecord::Migration[6.0] 2 def change 3 create_table :facilities do |t| 4 t.string :name, null: false 5 t.timestamps 6 end 7 end 8end
room_have migration
ruby
1class CreateRoomHaves < ActiveRecord::Migration[6.0] 2 def change 3 create_table :room_haves do |t| 4 t.references :room, foreign_key: true 5 t.references :facility, foreign_key: true 6 t.timestamps 7 end 8 end 9end
試したこと
エラー文からName errorと推測してclass名を確認するもミスが見つからず、collection_check_boxをコメントアウトするとcheck_box以外は正常に表示されデータベースへの保存も可能でした。
何かお気づきの点があればアドバイス頂けると幸いです
補足情報(FW/ツールのバージョンなど)
コンソールのエラー情報
ruby
1ActionView::Template::Error (uninitialized constant Room::RoomHafe): 2 30: <div class='facility-contents'> 3 31: 4 32: <%= f.collection_check_boxes :facility_ids, Facility.all, :id, :name, include_hidden: false do |b| %> 5 33: <% b.label {b.check_box + b.text} %> 6 34: <% end %> 7 35: 8 36: 9 10app/views/rooms/new.html.erb:33 11app/views/rooms/new.html.erb:33 12app/views/rooms/new.html.erb:32 13app/views/rooms/new.html.erb:6
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/05/29 07:02
2021/05/29 08:13
2021/05/29 08:33