前提
project>room>messageという感じで2段階でネストしてprojectの中にチャット機能を実装しています。
room#indexにルーム名一覧を表示し、そこからmessage#indexに遷移する際にエラーが発生します。
発生している問題・エラーメッセージ
No route matches {:action=>"index", :controller=>"messages", :project_id=>nil, :room_id=>"1"}, missing required keys: [:project_id]
該当のソースコード
config>routes.rb
1Rails.application.routes.draw do 2 devise_for :users 3 root to: "projects#index" 4 resources :users, only: [:edit, :update, :show] 5 resources :projects, only: [:new, :create, :show] do 6 resources :tasks, only: [:index, :new] 7 resources :donations, only: [:index, :create] 8 resources :rooms, only: [:index, :new, :create, :destroy] do 9 resources :messages, only: [:index, :create] 10 end 11 end 12end
app>controllers>rooms_controller.rb
1class RoomsController < ApplicationController 2 def index 3 @project = Project.find(params[:project_id]) 4 end 5 6 def new 7 @room = Room.new 8 end 9 10 def create 11 @prject = Project.find(params[:project_id]) 12 @room = Room.new(room_params) 13 if @room.save 14 redirect_to project_rooms_path(current_user) 15 else 16 render :new 17 end 18 end 19 20 def destroy 21 room = Room.find(params[:id]) 22 room.destroy 23 redirect_to root_path 24 end 25 26 private 27 28 def room_params 29 params.require(:room).permit(:room_name, user_ids: []).merge(project_id: @prject.id) 30 end 31 32end 33
app>views>messages>index.html.erb
1<div class="wrapper"> 2 <div class="side-bar"> 3 <%= render "side_bar" %> 4 </div> 5 <div class="chat"> 6 <%= render "main_chat" %> 7 </div> 8</div>
app>views>messages>_side_bar.html.erb
1 <% current_user.rooms.each do |room| %> 2 <div class="room"> 3 <div class="room-name"> 4 <%= link_to room.room_name, project_room_messages_path(@project, room) %> 5 </div> 6 </div> 7 <% end %>
試したこと
エラー画面のURLでは以下のようにproject_idも取得できているのですがなぜかエラー画面ではproject_id=>nilになってしまいます。
http://localhost:3000/projects/1/rooms/1/messages
どなたかご教示いただけると幸いです。
回答1件
あなたの回答
tips
プレビュー