链接关联对象不允许我调用它们的属性 Rails
Chaining associated objects won't allow me to call their attributes Rails
我需要一些帮助。我一直在 Rails 创建一个博客站点来研究一些 gems 和其他技术,但我 运行 遇到了一个问题。我将部分评论调用到我的博客显示页面中,如下所示:
show.html.slim:
- if notice != nil
p#notice.alert.alert-success role='alert' = notice
h1 Blog Post
h4 Title: #{@blog_post.title}
h4 Author: #{@blog_post.user.name}
h4 Entry:
p = @blog_post.blog_entry
= render partial: 'comments/form', locals: { comment: @comment }
.panel.panel-default
.panel-heading
h4 Comments
#js-comments.panel-body
= render partial: 'comments/comment', collection: @blog_post.comments
= link_to 'Feed', blog_posts_path, class: 'btn btn-primary'
问题出在 comments/comment 部分:
.panel-body
p
strong
= comment.user.username
| said at
span.posted-at
= comment.created_at.strftime('%m/%d/%Y %H:%M:%S')
| :
p = comment.comment_entry
- if policy(comment).update?
= link_to 'Edit Comment', edit_blog_post_comment_path(comment.blog_post, comment), class: 'btn btn-primary'
- if policy(comment).destroy?
= link_to 'Destroy Comment', [comment.blog_post, comment], method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-primary'
现在,当我调用 comment.user.username 或 comment.created_at.strftime 时,我收到 NoMethodError 消息,指出 username 和 strftime 是未定义的方法。但是,如果我调用 comment.user 和 comment.created_at,它就可以正常工作。出于某种原因,它不允许我将该附加方法添加到链中。我可以验证用户、blog_post 和评论之间是否存在所有正确的关联。
以防万一,这是我的展示页面博客控制器:
def show
@comment = @blog_post.comments.new
end
如有任何帮助,我们将不胜感激。谢谢!
编辑:
错误信息是:
undefined method `username' for nil:NilClass
和 better_errors 告诉我问题出在
的评论部分
comment.user.username
并在调用部分的显示页面上
= render partial: 'comments/comment', collection: @blog_post.comments
谢谢!
在控制器中初始化的注释添加到@blog_post.comments,但它没有用户或created_at。只需将您的控制器更改为 @comment = Comment.new
我需要一些帮助。我一直在 Rails 创建一个博客站点来研究一些 gems 和其他技术,但我 运行 遇到了一个问题。我将部分评论调用到我的博客显示页面中,如下所示:
show.html.slim:
- if notice != nil
p#notice.alert.alert-success role='alert' = notice
h1 Blog Post
h4 Title: #{@blog_post.title}
h4 Author: #{@blog_post.user.name}
h4 Entry:
p = @blog_post.blog_entry
= render partial: 'comments/form', locals: { comment: @comment }
.panel.panel-default
.panel-heading
h4 Comments
#js-comments.panel-body
= render partial: 'comments/comment', collection: @blog_post.comments
= link_to 'Feed', blog_posts_path, class: 'btn btn-primary'
问题出在 comments/comment 部分:
.panel-body
p
strong
= comment.user.username
| said at
span.posted-at
= comment.created_at.strftime('%m/%d/%Y %H:%M:%S')
| :
p = comment.comment_entry
- if policy(comment).update?
= link_to 'Edit Comment', edit_blog_post_comment_path(comment.blog_post, comment), class: 'btn btn-primary'
- if policy(comment).destroy?
= link_to 'Destroy Comment', [comment.blog_post, comment], method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-primary'
现在,当我调用 comment.user.username 或 comment.created_at.strftime 时,我收到 NoMethodError 消息,指出 username 和 strftime 是未定义的方法。但是,如果我调用 comment.user 和 comment.created_at,它就可以正常工作。出于某种原因,它不允许我将该附加方法添加到链中。我可以验证用户、blog_post 和评论之间是否存在所有正确的关联。
以防万一,这是我的展示页面博客控制器:
def show
@comment = @blog_post.comments.new
end
如有任何帮助,我们将不胜感激。谢谢!
编辑:
错误信息是:
undefined method `username' for nil:NilClass
和 better_errors 告诉我问题出在
的评论部分comment.user.username
并在调用部分的显示页面上
= render partial: 'comments/comment', collection: @blog_post.comments
谢谢!
在控制器中初始化的注释添加到@blog_post.comments,但它没有用户或created_at。只需将您的控制器更改为 @comment = Comment.new