Ruby on rails RubyonRails入门教程中的注释错误

Ruby on rails RubyonRails入门教程中的注释错误,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.1,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.1,我正在通过官方网站上的入门教程学习RubyonRails 我唯一改变的是post-url映射指向/post/friendly-url,而不是/post/id 一切都很顺利,直到我尝试在帖子中添加评论,我收到以下错误 CommentsController中的命名错误#创建 这是我的密码 /app/controllers/comments\u controller.rb /apps/models/post.rb class Posttrue 验证:content,:presence=>true 验证

我正在通过官方网站上的入门教程学习RubyonRails

我唯一改变的是post-url映射指向/post/friendly-url,而不是/post/id

一切都很顺利,直到我尝试在帖子中添加评论,我收到以下错误

CommentsController中的命名错误#创建 这是我的密码

/app/controllers/comments\u controller.rb /apps/models/post.rb
class Posttrue
验证:content,:presence=>true
验证:友好,:存在=>true
验证:摘录,:presence=>true
验证:friendly,:with=>/^[^]的格式+$/
有很多:注释,:依赖=>:销毁
有很多:标签
接受:标记的\u嵌套的\u属性,\u,:允许\u销毁=>:true,
:如果=>proc{attrs}attrs.all{{k,v{v.blank?}
def至_参数
友好的
结束
结束

对不起,如果这是太多的信息,也请让我知道,如果我错过了什么。我什么都试过了,却找不到问题所在。提前感谢。

问题是您在
CommentController#create
中的
@post
nil
。也就是说,您提供的
参数[:id]
不正确,或者在数据库中找不到。我建议检查日志以查看
params[:id]
包含的内容,并查看是否与
find\u by\u-friendly
所期望的内容相匹配

如果
参数[:id]
看起来正确,我会使用rails控制台来尝试
帖子。通过友好的
查找并传入一些值,看看这是否适合您


如果
params[:id]
值看起来不正确,那么您的
form\u for()
调用
comments/\u form.html.erb
可能是错误的,查看友好插件的文档,了解如何进行正确调用。

我已经通过查看日志验证了帖子是否传递空值,但我无法理解为什么在我添加评论时应用程序没有提交帖子信息。您的评论控制器似乎没有设置@post,所以我不确定它会在comments/_form.html.erb中使用什么?
undefined method `comments' for nil:NilClass
app/controllers/comments_controller.rb:7:in `create'
class CommentsController < ApplicationController
    def create
        @post = Post.find_by_friendly(params[:id])
        @comment = @post.comments.create(params[:comment])
        redirect_to post_path(@post)
    end

    def destroy
        @post = Post.find_by_friendly(params[:id])
        @comment = @post.comments.find(params[:id])
        @comment.destroy
        redirect_to post_path(@post)
    end
end
class Comment < ActiveRecord::Base
    belongs_to :post

    attr_accessible :body, :commenter

    validates :body,  :presence => true
    validates :commenter,  :presence => true
end
<%= form_for([@post, @post.comments.build]) do |f| %>

<%= f.label :commenter, "Your Name:" %>
    <%= f.text_field :commenter, :placeholder => "Your Name..." %>
<span class="help-block">What would you like to be called.</span><br/>

<%= f.label :body, "Your Comment:" %>
    <%= f.text_area :body, :size => "60x12", :placeholder => "Your Comment..." %>
<span class="help-block">What's on your mind?</span><br/>

    <%= f.submit %>

<% end %>
<% @post.tags.build %>
<%= form_for(@post) do |post_form| %>
<legend>Post Form</legend>
<% if @post.errors.any? %>
    <div id="error_explanation">
        <h2 class="text-error"><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
        <ul>
            <% @post.errors.full_messages.each do |msg| %>
                <li class="text-error"><%= msg %></li>
            <% end %>
        </ul>
    </div>
<% end %>

<%= post_form.label :name, "Post Name:" %>
    <%= post_form.text_field :name, :placeholder => "Post Name..." %>
<span class="help-block">The title of the article.</span><br/>

<%= post_form.label :friendly, "Friendly URL:" %>
    <%= post_form.text_field :friendly, :placeholder => "Friendly URL..." %>
<span class="help-block">SEO friendly URL displayed as /posts/post-name.</span><br/>

<%= post_form.label :content, "Post Content:" %>
<%= post_form.text_area :content, :size => "60x12", :placeholder => "Main Content..." %>
<span class="help-block">HTML enabled article content.</span><br/>

<%= post_form.label :excerpt, "Post Excerpt:" %>
    <%= post_form.text_area :excerpt, :placeholder => "Post Excerpt..." %>
<span class="help-block">Description of post for index page. No HTML.</span><br/>

<h2>Tags</h2>
<%= render :partial => 'tags/form',
    :locals => {:form => post_form} %><br/>

    <%= post_form.submit %>

<% end %>
Nullpulse::Application.routes.draw do
    resources :posts do
        resources :comments
    end

    root :to => "home#index"
end
class Post < ActiveRecord::Base
    attr_accessible :content, :friendly, :name, :excerpt, :tags_attributes

    validates :name,  :presence => true
    validates :content, :presence => true
    validates :friendly, :presence => true
    validates :excerpt, :presence => true
    validates_format_of :friendly, :with => /^[^ ]+$/

    has_many :comments, :dependent => :destroy
    has_many :tags

    accepts_nested_attributes_for :tags, :allow_destroy => :true,
        :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }

    def to_param
        friendly
    end
end