我是rails的初学者,所以我开始关注http://guides.rubyonrails.org/getting_started.html的教程。当你需要发表评论但我无法弄清楚问题是什么时,我会陷入6.4部分。当我想创建一个评论我得到这个错误哦,是的我在Windows上
未定义的方法`标题'为#Comment:0x88e4038
这是我的评论控制器
class CommentsController < ApplicationController
http_basic_authenticate_with name: "dhh", password: "secret", only: :destroy
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(comment_params)
redirect_to article_path(@article)
end
def destroy
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to article_path(@article)
end
private
def comment_params
params.require(:comment).permit(:commenter, :body)
end
end
这是我的show.html.erb
<p>
<strong>Title:</strong>
<%= @article.title %>
</p>
<p>
<strong>Text:</strong>
<%= @article.text %>
</p>
<h2>Comments</h2>
<%= render @article.comments %>
<h2>Add a comment:</h2>
<%= form_for([@article, @article.comments.build]) do |f| %>
<p>
<%= f.label :commenter %><br>
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Edit Article', edit_article_path(@article) %> |
<%= link_to 'Back to Articles', articles_path %>
comment.html.erb
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<p>
<%= link_to 'Destroy Comment', [comment.article, comment],
method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>
这是路线
Rails.application.routes.draw do
resources :articles do
resources :comments
end
root 'welcome#index'
end
答案 0 :(得分:2)
我查看了教程,评论模型没有属性标题。 Rails正在抱怨,因为您正在尝试验证该标题&#39;注释模型中的属性:
validates :title, presence: true, length: { minimum: 5 }
删除此验证。