Home>
I want to solve

We are developing an application that allows you to post articles on ruby ​​on rails6.

While implementing the function to comment on posted articles, undefined method `id'for: article: Symbol I got the error, but I want to solve this.

I somehow understood that if there is a difficulty in writing, such as a strange space or an extra (), it will be misread when executing the code and this error will occur, but my code In the case of, I can't find out where the problem is.

Would you please help me ...

Error message https://gyazo.com/160787b95c6a9a037545897db822be63

code

articles_controller.rb

def show
    @article = Article.find (params [: id])
    @comment = Comment.new
    @comments = @ article.comments.includes (: user)
  end

comments_controller.rb

def create
    @comment = Comment.new (comment_params)
      redirect_to "/articles/#{comment.tweet.id}"
  end
  private
  def comment_params
    params.require (: comment) .permit (: post) .merge (user_id: current_user.id, article_id: params [: article.id])
  end

articles/show.html.erb

      <% = form_with (model: [@article, @comment], local: true) do | f |%>
            
            <label for = "comments-Area">Comment</label>
            <% = f.text_field: post, class: "comments-area", placeholder: "Enter comments", id: "comments-area"%>
            <% = f.submit "submit", class: "btn btn-primary"%>
            
        <% end%>
        
        <p>
          <% @ comments.each do | comments |%>
            <% = link_to comment.user.nickname, "/ users/# {comment.user_id}"%>:
            <% comments.post%>
          <% end%>
        </p>
      

routes.rb

Rails.application.routes.draw do
  devise_for: users
  root to:'articles # index'
  resources: articles do
    resources: comments, only :: create
  end
end
What I tried

・ Review the description of the view file → I tried several patterns, but the error statement did not change
・ Binding.pry → The entered information has been sent properly.
・ Check if there are any strange spaces or () throughout. → It does not seem to be a problem (because it is a beginner, there is a high possibility of oversight)

  • Answer # 1

    A quick glance of comments_controller.rbparams [: article.id])Is strange.
    If you read the error statement carefully, isn't this file name and line number written?

    def comment_params
      params.require (: comment) .permit (: post) .merge (user_id: current_user.id, article_id: params [: article.id]) # Here
    end

    Correctlyparams [: article_id]it might be.