gpt4 book ai didi

ruby-on-rails - 删除分配,未定义方法 `avatar?' 为 nil :NilClass

转载 作者:太空宇宙 更新时间:2023-11-03 18:13:18 33 4
gpt4 key购买 nike

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

这是指令。

Finally, we also have to modify the comment-listing view -- the app/views/comments/_comment.html.erb partial called in posts#show. Update the partial to look this:

<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?' for nil: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>

评论 Controller

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。错误仍然存​​在。

最好的问候。

最佳答案

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

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

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

关于ruby-on-rails - 删除分配,未定义方法 `avatar?' 为 nil :NilClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30219020/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com