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.
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
<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
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
class Comment < ApplicationRecord
belongs_to :article
validates :title, presence: true
validates :body, presence: true, length: { minimum: 10 }
end
schema
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
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
Mael Paquette is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.