删除赋值,未定义的方法`avatar?'为零:NilClass

时间:2015-05-13 15:17:36

标签: ruby-on-rails ruby ruby-on-rails-3 carrierwave

我是Ruby on Rails的新生,目前正在为我的项目(一个Reddit克隆)处理Deletion HTTP请求。到目前为止,我已经成功地在主题和帖子上实现了Destroy功能。现在我正在删除评论,我遇到了一个错误,我不确定如何处理。

这是指示。

  

最后,我们还必须修改评论列表视图 - 在帖子#show中调用的app / views / comments / _comment.html.erb部分。更新部分看看:

<div class="media">
  <div class="media-left">
    <%= image_tag(comment.user.avatar.small.url, class: "media-object") if comment.user.avatar? %>
  </div>
  <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>
</div>

更改前我的_comment.html.erb:

<% @comments.each do |comment| %>

  <p><%= comment.body %></p>
  <p><i><%= comment.user.name %></i></p>

<% end %>

按照指示修改代码后,我收到此错误:

  

NoMethodError at / topics / 4 / posts / 11 undefined method`avatar?'对于   零:NilClass

突出显示这一行:

<%= image_tag(comment.user.avatar.small.url, class: "media-object") if comment.user.avatar? %>

以下是 views / post / show.html.erb:

<h1><%= markdown_to_html @post.title %></h1>

<div class="row">
  <div class="col-md-8">
    <small>
      <%= image_tag(@post.user.avatar.tiny.url) if @post.user.avatar? %>
      submitted <%= time_ago_in_words(@post.created_at) %> ago by
      <%= @post.user.name %>
    </small>
    <p><%= markdown_to_html @post.body %></p>
    <p><%= image_tag(@post.image.post.url) if @post.image? %>

    <h1>Comments</h1>
    <!-- render the comments loop -->
    <%= render partial: 'comments/comment', locals: { topic: @topic, post: @post, comment: @comment } %>

    <!-- render the comments form if user is signed in -->
    <% if policy(@comment).create? %>
      <%= render partial: 'comments/form', locals: { topic: @topic, post: @post, comment: @comment } %>
    <% end %>

  </div>
  <div class="col-md-4">
    <% if policy(@post).edit? %>
      <%= link_to "Edit", edit_topic_post_path(@topic, @post), class: 'btn btn-success' %>
    <% end %>
    <% if policy(@post).destroy? %>
      <%= link_to "Delete Post", [@topic, @post], method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this post?' } %>
    <% end %>
  </div>
</div>

评论控制器

class CommentsController < ApplicationController
  def create
    # find topic by id
    @topic = Topic.find(params[:topic_id])
    # find post id through topic
    @post = @topic.posts.find(params[:post_id])
    # comments on post
    @comments = @post.comments

    @comment = current_user.comments.build(params.require(:comment).permit(:body, :post_id, :avatar))
    @comment.post = @post


    authorize @comment
    if @comment.save
      flash[:notice] = "Comment was created."
      redirect_to [@topic, @post]
    else
      flash[:error] = "Error saving the comment. Please try again."
      # must render the the page calling the form!!
      render 'posts/show'
    end
  end

  def new

  end

  def destroy
    @topic = Topic.find(params[:topic_id])
    @post = Post.posts.find(params[:post_id])
    @comment = @post.comments.find(params[:id])

    authorize @comment
    if @comment.destroy
      flash[:notice] = "Comment was removed."
      redirect_to [@topic, @post]
    else
      flash[:error] = "Comment couldn't be deleted. Try again."
      redirect_to [@topic, @post]
    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

  # CarrierWave method for attribute functionality
  mount_uploader :avatar, AvatarUploader

  # These methods check the role of a user in the database
  def admin?
    role == 'admin'
  end

  def moderator?
    role == 'moderator'
  end
end

我试图根据项目中其他图像的处理方式将我的comment.user.avatar.small.url更改为@ comment.user.avatar.small.url。错误仍然存​​在。

最好的问候。

1 个答案:

答案 0 :(得分:3)

这是错误意味着comment.usernil。如果你想修复它,你应该调查为什么会发生这种情况。

如果这是正确的情况(例如这是一个匿名用户),那么您可以使用以下内容处理它:

... if comment.user.try(:avatar?)