我是ruby on rails的新手,并且知道这是一个初学者错误。我正在尝试创建一个在帖子中显示用户评论的应用程序。我有一个评论部分指向我的帖子#show,并且当请求显示<%= image_tag(comment.user.avatar.small.url,class:“media-object”)时,如果comment.user.avatar? %GT;来自_comment.html.erb,我为nil获取了“未定义的方法'用户':NilClass。我已经阅读了有关stackoverflow的其他问题,我知道这说它找不到与评论中的帖子关联的用户,而且我需要在我的控制器中定义用户。我假设它是用于后置控制器,但我似乎无法用特定的语法来解决用户在帖子中显示注释。
#comments_controller
def create
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:post_id])
@comment = current_user.comments.new(comment_params)
@comment.post = @post
@comment.save
authorize @comment
redirect_to [@topic, @post]
end
#posts_controller
def show
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
end
#post model
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :topic
has_one :summary
has_many :comments
mount_uploader :image, ImageUploader
#comment model
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :user
#rendering comment partial in post#show
<h1><%= @post.markdown_title %></h1>
<div class="row">
<div class="col-md-8">
<small>
<%= image_tag(@post.image.thumb.url) if @post.image? %></br>
<%= 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><%= @post.markdown_body %></p>
</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="col-md-8">
<% if policy(Comment.new).create? %>
<%= render partial: 'comments/comment', locals: { comment: @comment } %>
<%= render partial: 'comments/form', locals: {topic: @topic, post: @post} %>
<% else %>
<h3>Sign up to Bloccit to post comments</h3>
<%= link_to "Sign up", new_user_registration_path, class: 'btn btn-primary' %></br></br>
<% end %>
#comment partial
<ul>
<% @post.comments.each do |c| %>
<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>
<li>
<p><%= c.body %></p>
</li>
</div>
<% end %>
另外,当我在posts_controller中输入@ post.user = @user时,错误更改为“未定义的方法'avatar'为nil:NilClass。感谢您的帮助!
答案 0 :(得分:1)
您错误地引用了块变量。将|c|
更改为|comment|
或将comment
引用更改为c
。
所以这样:
<%= image_tag(c.user.avatar.small.url, class: "media-object") if c.user.avatar? %>
答案 1 :(得分:1)
未定义的方法'user'代表nil:NilClass
错误是因为这行
<%= render partial: 'comments/comment', locals: { comment: @comment } %>
您没有@comment
show
方法初始化posts_controller
,而locals
的{{1}}传递comment
,comment.user
_comment.html.erb
}返回错误。
而且我也注意到<% @post.comments.each do |c| %>
你有c
这一行,所以你可以使用comment
代替<%= image_tag(c.user.avatar.small.url, class: "media-object") if c.user.avatar? %>
。
def rfind(lst, condition):
return next((len(lst) - n
for (n, i) in enumerate(reversed(lst), 1)
if condition(i)),
-1) # If we couldn't find anything, return -1
>>> lst = ['{b}', '[c]', '{a}', 'g']
>>> print rfind(lst, lambda i: '{' in i)
2
>>> print rfind(lst, lambda i: 'q' in i)
-1