首页 文章

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

提问于
浏览
1

undefined方法 avatar?' for nil:NilClass undefined method name'为nil:NilClass

嗨,我在部分中收到以下错误 . 我列出这两个的原因是因为在评论出导致第一条错误消息的行之后,我得到了第二个错误,这导致我认为问题不是专门用“avatar”或“name”,而是用其他东西,尽管我不知道是什么 . 在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>
      
<%= 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

    如果你得到了

    nil的未定义方法foo:NilClass

    它's that the thing you'重新调用你的方法是 nil .

    所以在你的情况下,你在零上调用 avatar?name .

    看看你的代码,很明显 comment.user 是(a)调用那些方法,因此(b)什么是零 .

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

  • 1

    这个问题被发现了 . 在部分渲染中

    comment: @comment
    

    应该

    comment: comment
    

相关问题