【问题标题】:undefined method `comments' for nil:NilClassnil:NilClass 的未定义方法“注释”
【发布时间】:2011-10-23 11:05:22
【问题描述】:

我正在尝试创建一种让用户对我的帖子发表评论的方法。目前,我的主页上显示了所有用户帖子,然后在用户个人资料中只有当前用户的帖子。我想拥有它,以便 cmets 仅出现在用户个人资料中的帖子上。我试图在用户配置文件中添加评论表单,但我得到了一个未定义的方法 `cmets' 用于 nil:NilClass 错误。

我的 cmets_controller 看起来像

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post])
    @comment = @post.comments.create(params[:comment])
    redirect_to post_path(@post)
end

我在用户配置文件中呈现了部分 (_comment_form.html.erb),看起来像

<h2>Add a comment:</h2>
<%= form_for ([@post, @post.comments.build]) do |f| %>
   <div class="field">
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
   </div>
   <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
   </div>
   <div class="actions">
    <%= f.submit %>
   </div>
<% end %>

我的评论模型看起来像

class Comment < ActiveRecord::Base
  belongs_to :post
end

我的帖子模型看起来像

class Post < ActiveRecord::Base
 attr_accessible :content

 belongs_to :user

 validates :content, :presence => true
 validates :user_id, :presence => true
 validates :user, :presence => true
 validates :title, :presence => true

 has_many :comments

 default_scope :order => 'posts.created_at DESC'
end

我的用户配置文件看起来像 show.html.erb

<table class="profile" summary="Profile information">
  <tr>
    <td class="main">
    <h1>
        <%= gravatar_for @user %>
        <%= @user.name %>
    </h1>
    <% unless @user.posts.empty? %>
        <table class="posts" summary="User posts">
            <%= render @posts %>
            <%= render 'comments/comment_form' %>
        </table>    
    <% end %>
    </td>
    <td class="sidebar round">
  <strong>Name</strong> <%= @user.name %><br />
  <strong>URL</strong>  <%= link_to user_path(@user), @user %><br />
  <strong>Tasks</strong> <%= @user.posts.count %>
    </td>
  </tr>
</table>

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 comments posts


    【解决方案1】:

    可能是你没有在你的控制器的new方法中初始化@post,它被用作nil。如果可行,请始终为您的新表单构建一个空模型:

    def new
      @post = Post.new(params[:post])
    end
    

    【讨论】:

      【解决方案2】:

      您是否在 PostsController 的 show 操作中初始化 @post?这将是必需的,因为您正在从 CommentsController 的创建操作重定向。

      【讨论】:

      • 是的,我尝试过这样做,但除非我做错了,否则它仍然没有改变任何东西。我确实 def show (at)post = Post.find(params[:post]) (at)cmets = (at)post.cmets end
      • 您是否在 PostsController 或 CommentsController 中添加了显示操作处理程序?您需要在 PostsController 中添加操作处理程序(如果有)
      【解决方案3】:

      您能看到log/development.log 以了解错误发生的位置吗?从问题中并不清楚。但是从你的代码来看,有两个可能的位置:

      1. @comment = @post.comments.create(params[:comment]) 这不太可能,因为最后一行代码是Post.find,如果找不到id,它将引发RecordNotFound

      2. &lt;%= form_for ([@post, @post.comments.build]) do |f| %&gt;

      这很有可能,你能做一个puts @post.inspect 并检查你的 development.log 看看它是否为空。假设它为空,你需要在你渲染_comment_form.html.erb的任何地方实例化一个Post对象

      【讨论】:

        【解决方案4】:
        <%= render @posts %>
        

        这一行应该引用@post。请注意尾随的 s,与代码中对它的所有其他引用相比。

        【讨论】:

          【解决方案5】:
          @post = Post.find_by_id(params[:post_id])
          

          【讨论】:

          • 欢迎来到 Stack Overflow!始终为您发布的代码提供解释,因为这将需要以供将来参考。
          猜你喜欢
          • 2011-07-27
          • 2013-06-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多