need help figuring out why i keep getting undefined method ‘comments’ in the ruby on rails blog tutorials

I’m trying out ruby on rails for a project and up to this point its been great but when following the tutorial and i get to 8, for comments, it just doesnt work? ive try to go back and reread, but i always get the undefined method with the comments being the part not working. the route is there so i dont know what is wrong as ive followed the tutorial perfectly (i think) for the comment part.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
NoMethodError in Articles#show
Showing C:/blog/app/views/articles/show.html.erb where line #17 raised:
undefined method `comments' for an instance of Article
16 <h2>Add a comment:</h2>
17 <%= form_with model: [ @article, @article.comments.build ] do |form| %>
18 <p>
19 <%= form.label :commenter %><br>
20 <%= form.text_field :commenter %>
</code>
<code> NoMethodError in Articles#show Showing C:/blog/app/views/articles/show.html.erb where line #17 raised: undefined method `comments' for an instance of Article 16 <h2>Add a comment:</h2> 17 <%= form_with model: [ @article, @article.comments.build ] do |form| %> 18 <p> 19 <%= form.label :commenter %><br> 20 <%= form.text_field :commenter %> </code>

NoMethodError in Articles#show
Showing C:/blog/app/views/articles/show.html.erb where line #17 raised:

undefined method `comments' for an instance of Article


16 <h2>Add a comment:</h2>
17 <%= form_with model: [ @article, @article.comments.build ] do |form| %>
18 <p>
19  <%= form.label :commenter %><br>
20  <%= form.text_field :commenter %>

here are my codes, which for the most part follow the tutorial

articles/show

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><h1><%= @article.title %></h1>
<p><%= @article.body %></p>
<br>
<br>
<h3>
<%= link_to "Edit", edit_article_path(@article) %> <br>
<%= link_to "Destroy", article_path(@article), data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
} %> <br>
<%= link_to "Home", root_path %>
</h3>
<h2>Comments</h2>
<% @article.comments.each do |comment| %>
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<% end %>
<h2>Add a comment:</h2>
<%= form_with model: [ @article, @article.comments.build ] do |form| %>
<p>
<%= form.label :commenter %><br>
<%= form.text_field :commenter %>
</p>
<p>
<%= form.label :body %><br>
<%= form.text_area :body %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
</code>
<code><h1><%= @article.title %></h1> <p><%= @article.body %></p> <br> <br> <h3> <%= link_to "Edit", edit_article_path(@article) %> <br> <%= link_to "Destroy", article_path(@article), data: { turbo_method: :delete, turbo_confirm: "Are you sure?" } %> <br> <%= link_to "Home", root_path %> </h3> <h2>Comments</h2> <% @article.comments.each do |comment| %> <p> <strong>Commenter:</strong> <%= comment.commenter %> </p> <p> <strong>Comment:</strong> <%= comment.body %> </p> <% end %> <h2>Add a comment:</h2> <%= form_with model: [ @article, @article.comments.build ] do |form| %> <p> <%= form.label :commenter %><br> <%= form.text_field :commenter %> </p> <p> <%= form.label :body %><br> <%= form.text_area :body %> </p> <p> <%= form.submit %> </p> <% end %> </code>
<h1><%= @article.title %></h1>

<p><%= @article.body %></p>
<br>
<br>
<h3>
<%= link_to "Edit", edit_article_path(@article) %>  <br> 
<%= link_to "Destroy", article_path(@article), data: {
    turbo_method: :delete,
    turbo_confirm: "Are you sure?"
  } %>  <br>
<%= link_to "Home", root_path %>
</h3>

<h2>Comments</h2>
<% @article.comments.each do |comment| %>
  <p>
    <strong>Commenter:</strong>
    <%= comment.commenter %>
  </p>

  <p>
    <strong>Comment:</strong>
    <%= comment.body %>
  </p>
<% end %>

<h2>Add a comment:</h2>
<%= form_with model: [ @article, @article.comments.build ] do |form| %>
  <p>
    <%= form.label :commenter %><br>
    <%= form.text_field :commenter %>
  </p>
  <p>
    <%= form.label :body %><br>
    <%= form.text_area :body %>
  </p>
  <p>
    <%= form.submit %>
  </p>
<% end %>

comments_controller

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class CommentsController < ApplicationController
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(comment_params)
redirect_to article_path(@article)
end
private
def comment_params
params.require(:comment).permit(:commenter, :body)
end
end
</code>
<code>class CommentsController < ApplicationController def create @article = Article.find(params[:article_id]) @comment = @article.comments.create(comment_params) redirect_to article_path(@article) end private def comment_params params.require(:comment).permit(:commenter, :body) end end </code>
class CommentsController < ApplicationController
  def create
    @article = Article.find(params[:article_id])
    @comment = @article.comments.create(comment_params)
    redirect_to article_path(@article)
  end

  private
    def comment_params
      params.require(:comment).permit(:commenter, :body)
    end
end

models/comments

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Comment < ApplicationRecord
belongs_to :article
validates :title, presence: true
validates :body, presence: true, length: { minimum: 10 }
end
</code>
<code>class Comment < ApplicationRecord belongs_to :article validates :title, presence: true validates :body, presence: true, length: { minimum: 10 } end </code>
class Comment < ApplicationRecord
  belongs_to :article
  
  validates :title, presence: true
  validates :body, presence: true, length: { minimum: 10 }
end

schema

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>ActiveRecord::Schema[7.1].define(version: 2024_06_07_161059) do
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "comments", force: :cascade do |t|
t.string "commenter"
t.text "body"
t.integer "article_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["article_id"], name: "index_comments_on_article_id"
end
add_foreign_key "comments", "articles"
end
</code>
<code>ActiveRecord::Schema[7.1].define(version: 2024_06_07_161059) do create_table "articles", force: :cascade do |t| t.string "title" t.text "body" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "comments", force: :cascade do |t| t.string "commenter" t.text "body" t.integer "article_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["article_id"], name: "index_comments_on_article_id" end add_foreign_key "comments", "articles" end </code>
ActiveRecord::Schema[7.1].define(version: 2024_06_07_161059) do
  create_table "articles", force: :cascade do |t|
    t.string "title"
    t.text "body"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "comments", force: :cascade do |t|
    t.string "commenter"
    t.text "body"
    t.integer "article_id", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["article_id"], name: "index_comments_on_article_id"
  end

  add_foreign_key "comments", "articles"
end

routes

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Rails.application.routes.draw do
root "articles#index"
resources :articles do
resources :comments
end
end
</code>
<code>Rails.application.routes.draw do root "articles#index" resources :articles do resources :comments end end </code>
Rails.application.routes.draw do
  root "articles#index"

  resources :articles do
    resources :comments
  end
end

here’s a screenshot of the page when i try to access an article

ive tried going back and copying everything again, ive trying modifying some part of the code, but im not currently really familiar with ruby so im not really sure where to look, and nothing i found online is excactly my problem

New contributor

Mael Paquette is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật