前提
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
Rails.application.routes.draw do devise_for :users root to: "projects#index" resources :users, only: [:edit, :update, :show] resources :projects, only: [:new, :create, :show] do resources :tasks, only: [:index, :new] resources :donations, only: [:index, :create] resources :rooms, only: [:index, :new, :create, :destroy] do resources :messages, only: [:index, :create] end end end
app>controllers>rooms_controller.rb
class RoomsController < ApplicationController def index @project = Project.find(params[:project_id]) end def new @room = Room.new end def create @prject = Project.find(params[:project_id]) @room = Room.new(room_params) if @room.save redirect_to project_rooms_path(current_user) else render :new end end def destroy room = Room.find(params[:id]) room.destroy redirect_to root_path end private def room_params params.require(:room).permit(:room_name, user_ids: []).merge(project_id: @prject.id) end end
app>views>messages>index.html.erb
<div class="wrapper"> <div class="side-bar"> <%= render "side_bar" %> </div> <div class="chat"> <%= render "main_chat" %> </div> </div>
app>views>messages>_side_bar.html.erb
<% current_user.rooms.each do |room| %> <div class="room"> <div class="room-name"> <%= link_to room.room_name, project_room_messages_path(@project, room) %> </div> </div> <% end %>
試したこと
エラー画面のURLでは以下のようにproject_idも取得できているのですがなぜかエラー画面ではproject_id=>nilになってしまいます。
http://localhost:3000/projects/1/rooms/1/messages
どなたかご教示いただけると幸いです。
0 コメント