Home>

You can post a message in the room, and clicking the message will take you to the comment posting page.
So when I try to post a comment, I get the following error message:

How do I save my comments successfully?

Error message
ActiveRecord :: RecordNotFound in CommentsController # index
Couldn't find Message without an ID
Corresponding source code
[Comments.controller.rb]
class CommentsController</pre>
<pre><code data-language = "Ruby">[Index.html.erb (comment)]

      
    <% = form_with model: @comment, url: room_message_comments_path (@room, @messages), local: true do | f |%>
      <% = f.text_area: text, placeholder: "comment", class: "comment-text"%>
    <p>
        Think about the other person and try to make polite comments.
      <br>
        Unpleasant language may result in usage restrictions or withdrawal.
    </p>
    <% = f.submit "comment"%>
   <% end%>
[Messages.controller.rb]
class MessagesController</pre>
<pre><code data-language = "Ruby">[Routes.rb]
Rails.application.routes.draw do
  devise_for: users
  get'messages/index'
  get'comments/index'
  root to: "rooms # index"
  resources: users
  resources: rooms do
    resources: messages do
      resources: comments
    end
  end
end
【log】
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
[Index.html.erb (message)]
(abridgement)
(Path part for transitioning to comment.new.html)
    
    <% = link_to message.content, new_room_message_comment_path (@room, @messages)%>
    
(abridgement)
[Gyazo on error message screen]
https://gyazo.com/8b7f3ec749368907f777596b6ec6115d
  • Answer # 1

    You're confused.
    The error message tells you that CommentsController # index does not pass params [: id].
    To chase the cause
    "You can post a message in the room, and clicking on that message will take you to the comment posting page."
    There is, but the view on it is [index.html.erb (comment)].
    It's not new, but you're posting with this, right?
    Then the view that calls the index of CommentsController and the action that calls it
    The question is whether you have params [: id].
    Judging from "You can post a message in the room, and clicking the message will take you to the comment posting page", is it room?
    I can't judge because there is neither a view nor a controller.

    So

    Is it correct that the comment is entered by index instead of new?

    Please put the code of the view that calls the index of CommentsController and the action that calls it.