前提・実現したいこと
本の感想アプリでDM機能を作っています。
chatを始めるボタンを押すと下記のエラーが発生しました。
発生している問題・エラーメッセージ
Routing Error No route matches [GET] "/rooms"
該当のソースコード
/bookers2-debug/app/views/users/show.html.erb
<% unless @user.id == current_user.id %> <% if (current_user.following? @user) && (@user.following? current_user) %> <% if @isRoom == true %> <p class="user-show-room"><a href="/rooms/#{@room.id}" class="btn btn-primary btn-lg">chatへ</a> <% else %> <%= form_with @room, local: true do |f| %> <%= fields_for @entry do |e| %> <%= e.hidden_field :user_id, :value=>@user.id %> <% end %> <%= f.submit "chatを始める", class:"btn btn-primary btn-lg user-show-chat "%>(ここのボタンを押すとエラーが出ました。) <% end %> <% end %> <% end %> <% end %>
/bookers2-debug/app/controllers/users_controller.rb
def show @user = User.find(params[:id]) @books = @user.books @book = Book.new @currentUserEntry=Entry.where(user_id: current_user.id) @userEntry=Entry.where(user_id: @user.id) unless @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 unless @isRoom @room = Room.new @entry = Entry.new end end end
/bookers2-debug/app/controllers/rooms_controller.rb
before_action :authenticate_user! def create @room=Room.create @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) end end
/bookers2-debug/config/routes.rb
Rails.application.routes.draw do get 'searches/search' get 'relationships/create' get 'relationships/destroy' get 'book_comments/create' get 'book_comments/destroy' devise_for :users root 'homes#top' get 'home/about' => 'homes#about' resources :users,only: [:show,:index,:edit,:update] do resource :relationships,only: [:create, :destroy] get 'followings' => 'relationships#followings' get 'followers' => 'relationships#followers' end resources :books do resources :book_comments, only: [:create, :destroy] resource :favorites,only:[:create,:destroy] end resources :messages,only:[:create] resources :rooms,only:[:create, :show] end
ターミナルのルーテイングの出力
rooms POST /rooms(.:format) rooms#create room GET /rooms/:id(.:format) rooms#show
試したこと
/bookers2-debug/app/views/users/show.html.erbのform_withヘルパーに問題があると考え,
urlの追加、メソッドの追加などを行ったが治らなかった。
ルートパスやURLの誤字の確認
補足情報(FW/ツールのバージョンなど)
ruby '2.6.3'
gem 'rails', '~> 5.2.5'
以下が参考にした記事になります。
https://qiita.com/nojinoji/items/2b3f8309a31cc6d88d03
同じ記事を参考にした方で全く同じ質問をされていた方がおられましたが、解決には至っていませんでした。。。
ご教授お願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/08/01 07:03
2021/08/01 11:29