【问题标题】:Undefined method, avatar and name for nil:NilClassnil:NilClass 的未定义方法、头像和名称
【发布时间】:2014-05-05 07:09:23
【问题描述】:

nil:NilClass 的未定义方法 avatar?' for nil:NilClass undefined methodname'

您好,我收到以下部分错误。我列出两者的原因是因为在注释掉导致第一条错误消息的行之后,我得到第二个错误,这使我相信问题不在于“头像”或“名称”,而是与其他东西有关,尽管我不知道是什么。在 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>

以下是我的用户模型和 cmets_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

评论控制器

  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

【问题讨论】:

  • nil:NilClass 错误通常是因为您正在调用未处理的变量您正在使用的 var 内部包含 nil 数据
  • 您能告诉我们您的UserComment 模型吗?
  • 用户未设置评论就是它所说的。为什么?那是上游的某个地方。 current_user.cmets.build(comment_params) 周围是我要看的地方。
  • 等等,如果您在控制器中设置@comment,您的视图中如何设置comment?你不应该使用@comment吗?
  • @RichPeck - 我添加了评论和用户模型。我怀疑问题出在调用的方法上,但就像我在上面写的那样,在 rails 控制台中,所有 cmets 都有一个 user_id。另外,我是新手,不知道还有什么好找的

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


【解决方案1】:

如果你得到了

未定义方法foo 用于nil:NilClass

您调用方法的对象是nil

所以在你的情况下,你在 something nil 上调用 avatar?name

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

结果:您的评论没有用户。要么强制所有 cmets(包括新的/空的/存根的)有一个用户(空白用户?),或者让你的视图不需要用户。

【讨论】:

  • 我添加了评论和用户模型。我怀疑问题出在调用的方法上,但就像我在上面写的那样,在 rails 控制台中,所有 cmets 都有一个 user_id。另外,我是新手,不知道还有什么好找的
  • @user3465296 在您的情况下,我会使用pry (gem pry-rails) 并在异常之前的视图中插入&lt;% binding.pry %&gt;
【解决方案2】:

问题已被发现。在部分渲染中

comment: @comment

应该是

comment: comment

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多