前提
ruby on railsで掲載されたものに応募をすると掲載者とDMできる機能を作成しています。
plans_controller.rbのshowアクションでcurrentUserEntryと@userEntryを作成しました。
それぞれ自身と掲載者のidを入れています、
ですが、応募ボタンを押した際に掲載者のデータが入っていませんでした、
応募ボタンは掲載ごとにしたいので、応募した際に<%= e.hidden_field :plan_id, value: @plan.id %>でplan(掲載)のデータを送るようにしています。
実現したいこと
1.DMの作成がユーザーが同じだと、違う掲載の応募でも同じroomを使用することになってしまうのですが、これを同じ掲載者、違う掲載の場合は改めてroomを作るようにしたいです。
2.掲載者のデータが入っていないので、roomに掲載者のデータも入れたい。
発生している問題・エラーメッセージ
plan.idから得たplanの中のuser_idを@userEntryに入れるには
どうすればいいかわからず手詰まっています。
エラーはなく、@userEntryに@plan.user_idが入っていないのでもしかしたらuser_idがnilの可能性があります。
該当のソースコード
plan.controller.rb showアクション
def show if user_signed_in? @plan=Plan.find(params[:id]) @currentUserEntry=Entry.where(user_id: current_user.id) @userEntry=Entry.where(user_id: @plan.user_id) unless @plan.user.id == current_user.id @currentUserEntry.each do |cu| @userEntry.each do |u| if cu.room_id == u.room_id then @isRoom = true @roomId = cu.room_id end end end if @isRoom else @room = Room.new @entry = Entry.new end end end end
rooms_controller.rb createアクション
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
試したこと
(user_id: @plan.id)などにして一度planごと入れてみようとしたが、
変化は起きませんでした。
あなたの回答
tips
プレビュー