困っている箇所
rooms_controllerのindex箇所の掲載者のuserを入れる@anotherEntriesの箇所でエラーが起こってしまいました。
undefined method `user_id' for nil:NilClass
エラー
NoMethodError at /rooms undefined method `user_id' for nil:NilClass Hint: Something is `nil` when it probably shouldn't be.
rooms_controller
class RoomsController < ApplicationController before_action :authenticate_user! def index @currentEntries = current_user.entries myRoomIds = [] @currentEntries.each do |entry| myRoomIds << entry.room.id end @anotherEntries = Entry.where(room_id: myRoomIds).where('user_id != ?',@plan.user_id) end def create @plan = Plan.find(params[:entry][:plan_id]) @room = Room.create(room_name: @plan.title) @entry1 = Entry.create(room_id: @room.id, user_id: current_user.id) @entry2 = Entry.create(params.require(:entry).permit(:user_id, :room_id).merge(room_id: @room.id)) redirect_to "/rooms/#{@room.id}" end def show @room = Room.find(params[:id]) if Entry.where(user_id: current_user.id,room_id: @room.id).present? @messages = @room.messages @message = Message.new @entries = @room.entries else redirect_back(fallback_location: root_path) end end private def set_plan @plan = Plan.find(params[:id]) end end
room.rb
class Room < ApplicationRecord has_many :entries, dependent: :destroy has_many :messages, dependent: :destroy has_many :users has_many :plans end
views/rooms/index.html.erb
<div message-list> <h4>メッセージ一覧</h4> <% @my_rooms.each do |room| %> <p><%= render 'rooms', rooms: @rooms %></p> <% end %> </div>
質問
@anotherEntries = Entry.where(room_id: myRoomIds).where('user_id != ?',@plan.user_id)
で掲載者のuserを入れたいのですが、user_idがnilの為、@plan = Plan.find(params[:id])でplan_idからplanを抜き出し、さらにそこからplan.user_idを抜き出して@anotherEntries のuser_idに代入させようとしたのですが、
ActiveRecord::RecordNotFound at /rooms Couldn't find Plan without an ID
と表示され,調べたところルーティングに関しての情報が多くあったのですが、
ルーティングを見ても問題ないと思います。
@plan = Plan.find(params[:id])でレコードから抜き出すやり方は正しいでしょうか?
!追記
routes.rb
Rails.application.routes.draw do devise_for :users, controllers: { registrations: 'users/registrations' } root to: 'toppages#index' get "users/show" => "users#show" devise_scope :users do get '/users', to: redirect("/users/sign_up") end resources :users do member do get :followings get :followers get :likes end collection do get :search end end resources :users resources :plans resources :relationships, only: [:create, :destroy] resources :favorites, only: [:create, :destroy] resources :messages, only: [:create] resources :rooms end
routes.rb
rooms GET /rooms(.:format) rooms#index POST /rooms(.:format) rooms#create new_room GET /rooms/new(.:format) rooms#new edit_room GET /rooms/:id/edit(.:format) rooms#edit room GET /rooms/:id(.:format) rooms#show PATCH /rooms/:id(.:format) rooms#update PUT /rooms/:id(.:format) rooms#update DELETE /rooms/:id(.:format) rooms#destroy

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
退会済みユーザー
2022/10/05 02:01 編集
2022/10/05 02:10
退会済みユーザー
2022/10/05 04:56 編集
2022/10/05 04:59
退会済みユーザー
2022/10/05 05:09
2022/10/05 05:16
退会済みユーザー
2022/10/05 05:28
2022/10/05 05:32
退会済みユーザー
2022/10/05 05:41
2022/10/05 05:43
退会済みユーザー
2022/10/05 06:07 編集
退会済みユーザー
2022/10/05 06:07 編集
2022/10/05 06:04
退会済みユーザー
2022/10/05 06:08