Home>

I am making a chat room app. It's as simple as creating a room and exchanging messages with other users in it.

【problem】
Clicking on a message posted in the room will take you to the comments page
Therefore, when I try to post a comment by typing characters in the comment field, an unknown action error occurs.

[Question] I thought that the new action and create action would work because I posted a comment and saved it in the table, but why is the update method working? I would be very grateful if you could tell me here. And how are comments saved?

Error message
Unknown action
The action'update' could not be found for RoomsController
Corresponding source code
[New.html.erb (comment)]
    <% if current_user%>  <% = form_with (model: [@room],

 local: true) do | f |%>    <% = f.text_area: text, placeholder: "comment", rows: "2"%>    <% = f.submit "SEND"%>  <% end%><% else%>  <strong><p>* * * New registration/login is required to post comments * * *</p></strong><% end%>
[Comments.controller.rb]
class CommentsController</pre><pre><code data-language = "Ruby">[_Main_chat.html.erb]          <% = @ room.name%>            <% if current_user.genre == "teacher"%>  <% = link_to "Delete room", room_path (@room), method :: delete%><% end%>              <% = message.user.name%>          <% = l message.created_at%>              <% = link_to message.content, new_room_message_comment_path (@room, @messages)%>     <% = image_tag message.image.variant (resize: '400x400'), class:'message-image' if message.image.attached?%><% = form_with model: [@room, @message],

 class:'form', local: true do | f |%>  <% = f.text_field: content, class:'form-message', placeholder:'type a message'%><label>  <span>image</span>  <% = f.file_field: image, class:'hidden'%></label><% = f.submit'submit', class:'form-submit'%><% end%>
[Routes.rb]
Rails.application.routes.draw do
  devise_for: usersget'messages/index'
  get'comments/index'
  root to: "rooms # index"
  resources: users
  resources: rooms do
    resources: messages do
      resources: comments
    end
  end
end
[Coment.rb]
class Comment</pre>
<pre><code data-language = "Ruby">[Rooms.controller.rb]
class RoomsController</pre>
<pre><code data-language = "Ruby">              room_message_comments GET/rooms /: room_id/messages /: message_id/comments (.: format) comments # index
                                      POST/rooms /: room_id/messages /: message_id/comments (.: format) comments # create
             new_room_message_comment GET/rooms /: room_id/messages /: message_id/comments/new (.: format) comments # new
                 room_message_comment GET/rooms /: room_id/messages /: message_id/comments /: id (.: format) comments # show

Ruby

`` ```

  • Answer # 1

    form_with (model: [@room],That's why.
    @room is an "existing" "instance of Room", isn't it?
    Therefore, the update of RoomsController is called.