Home>

Please describe your question here in detail.
I am making a chat application with Ruby on Rails.
There are four tables below
① User table
② Room table
③ Message table
④ Intermediate table between room and user

I implemented the function of index action and show action of the room controller.
In the process, we have added a link to the show action page on the view page of incex.
There seems to be no problem in the description of index, but an error message occurred that the content of the id of the show action of rooms_controller is index.
When I try to access the view page of index.html.erb, I get the following error message:
I would like to know why this happens.

Couldn't find Room with'id'=index
Extracted source (around line #22):
  def show
    @room = Room.find(params[:id])
    @messages = @room.messages.includes(:user).order(:id).last(100)
    @message = current_user.messages.build
  end
rooms_controller.rb
class RoomsController</pre>
<strong>index.html.erb</strong>
<pre><code data-language="Ruby">class RoomsController 
<ul>
  <% @rooms.each do |room| %>
    <li><%= link_to "#{room.name}", room_path(room.id), class:"family_button" %></li>
  <% end %>
</ul>
new.html.erb
">
<%= render @messages %>

<%= render'footer' %>
routes.rb
Rails.application.routes.draw do
  mount ActionCable.server =>"/cable"
  resources :rooms, only: [:index, :new, :show, :create, :update] do
    resources :messages, only: [:create]
  end
  root to: "users#show"

  devise_for :users, controllers: {
    registrations: "users/registrations",
    sessions: "users/sessions"
  }
  resources :users, only: [:show]

  get "/show_additionally", to: "rooms#show_additionally"
  devise_scope :user do
    get "sign_in", to: "users/sessions#new"
    get "sign_out", to: "users/sessions#destroy"
  end
end
What I tried
  • change root_path from index to show
  • I tried setting room.id to be the room passed as an argument of index.html.erb
  • I changed (params[:id]) of rooms_controller to (:id room.id)
Supplemental information (FW/tool ​​version, etc.)
  • Ruby ver 2.6.5
  • Rails ver 6.0.0
  • Answer # 1

    room_path(room.id)

    Since show is one data, it is singular.
    At the terminalrails routesYou can check the description of the path to skip to the show action by executing.

  • Answer # 2

    Maybe/rooms/indexWould you like to access to?

    standardresourcesIn case of Routing made byindexThe URL to access the action is/roomsis.

    /rooms/indexIs the error,Rooms#showsoid=indexIs treated as

  • Answer # 3

    You mean you want to go to the index page, right?

    http://localhost:3000/rooms/index

    I think it will be a show action when you access.

    If you want to connect to the index action, it's /rooms/.
    The helper to use is rooms_path.