未定义的方法,头像和名称为nil:NilClass

时间:2014-03-26 17:20:48

标签: ruby-on-rails methods undefined null avatar

未定义的方法avatar?' for nil:NilClass undefined method名称'为零:NilClass

嗨,我在部分内容中收到以下错误。我列出这两个的原因是因为在评论出导致第一条错误消息的行之后,我得到第二个错误,这导致我认为问题不是" avatar"或"名称"特别是,但有其他东西,虽然我不知道什么。在rails控制台中,我可以在评论中呼叫用户和姓名。如果重要,我还使用Faker播种数据库。这是部分的。

<%= content_tag :div, class: 'media', id: "comment-#{comment.id}" do %>
  <%= link_to '#', class: 'pull-left' do %>
    <%= image_tag(comment.user.avatar.small.url) if comment.user.avatar?  %>
  <% end %>
  <div class="media-body">
    <small>
      <%= comment.user.name %> commented <%= time_ago_in_words(comment.created_at) %> ago
      <% if policy(comment).destroy? %>
        | <%= link_to "Delete", [@topic, @post, comment], method: :delete %>
      <% end %>
    </small>
    <p><%= comment.body %></p>
  </div>
<% end %>

另外,请参阅渲染。

  <div class="col-md-4">
    <% if policy(Comment.new).create? %>
      <h4>Leave a comment</h4>
      <br/>
      <%= render partial: 'comments/comment', locals: { topic: @topic, post: @post, comment: @comment } %>
    <% end %>
</div>

以下是我的用户模型和comments_controller

class UsersController < ApplicationController
  before_filter :authenticate_user!

  def update
    if current_user.update_attributes(user_params)
      flash[:notice] = "User information updated"
      redirect_to edit_user_registration_path(current_user)
    else
      render "devise/registrations/edit"
    end
  end

  private

  def user_params
    params.require(:user).permit(:name, :avatar)
  end
end

Comments_controller

  def create
    @topic = Topic.find(params[:topic_id])
    @post = @topic.posts.find(params[:post_id])
    @comments = @post.comments

    @comment = current_user.comments.build(comment_params)
    @comment.post = @post
    @new_comment = Comment.new

    authorize @comment
    if @comment.save
      redirect_to [@topic, @post], notice: "Comment was submitted successfully."
    else
      flash[:error] = "There was an error submitting the comment. Please try again."
    end
  end

我已经重置了数据库,但无济于事。坚持问题是什么。谢谢你的帮助。

请参阅下面的我的用户和评论模型。

class Comment < ActiveRecord::Base
  belongs_to :post
  belongs_to :user

  default_scope { order('created_at DESC') }

  validates :body, length: { minimum: 5 }, presence: true

  after_create :send_favorite_emails

  private

  def send_favorite_emails
    self.post.favorites.each do |favorite|
      if favorite.user_id != self.user_id && favorite.user.email_favorites?
        FavoriteMailer.new_comment(favorite.user, self.post, self).deliver
      end
    end
  end
end

用户模型

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  has_many :posts
  has_many :comments
  has_many :votes, dependent: :destroy
  has_many :favorites, dependent: :destroy
  mount_uploader :avatar, AvatarUploader

  def role?(base_role)
    role == base_role.to_s
  end

  def favorited(post)
    self.favorites.where(post_id: post.id).first
  end

  def voted(post)
    self.votes.where(post_id: post.id).first
  end

  private
end

2 个答案:

答案 0 :(得分:1)

如果你正在

  

foo

的未定义方法nil:NilClass

你正在调用你的方法的东西是nil

因此,在您的情况下,您在某事上调用avatar?name

查看您的代码,明确comment.user是(a)调用这些方法的内容,因此(b)什么是nil。

结果:您的评论没有用户。是强制执行所有注释(包括新/空/存根注释)以使用户(空白用户?),或者进行查看以便不需要用户。

答案 1 :(得分:0)

问题被发现了。在部分渲染

comment: @comment

应该是

comment: comment