【问题标题】:undefined method `comments' for #<Post::ActiveRecord_Relation:0x58ce72>#<Post::ActiveRecord_Relation:0x58ce72> 的未定义方法“评论”
【发布时间】:2018-03-08 21:08:09
【问题描述】:

我不断收到未定义的“评论”方法,看起来一切都很好,但这里没有运气是所涉及的代码

型号

class Post < ApplicationRecord
  validates :user_id, presence: true
  belongs_to :user
  has_many :comments, dependent: :destroy
  validates :image, presence: true

  has_attached_file :image, styles: { :medium => "640x" }
  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/

end

class Comment < ApplicationRecord

  belongs_to :post
  belongs_to :user

end

控制器

---Comments Controller----
def create
  @post = Post.find(params[:post_id])
  @comment = @post.comments.build(comment_params)
  @comment.user_id = current_user.id

  if @comment.save
    create_notification @post, @comment
    respond_to do |format|
      format.html { redirect_to root_path }
      format.js
    end
  else
    flash[:alert] = 'Check the comment form, something went wrong.'
    render root_path
  end
end

-----涉及后控制器--------

def index
 @post = Post.all
end

查看

<div class="comment-form">
  <%= form_for([@post, @post.comments.build ], local:true) do |form| %>
    <%= f.text_field :content, placeholder: 'Add a comment...' %>
  <% end %>
</div>

错误

NoMethodError in Posts#index
Showing C:/Sites/Womberry2/app/views/posts/index.html.erb where line #56 raised:
undefined method `comments' for #<Post::ActiveRecord_Relation:0x58ce720>
Extracted source (around line #56):

    <div class="comment-form">
    <%= form_for([@post, @post.comments.build ], local:true) do |form| %>
    <%= f.text_field :content, placeholder: 'Add a comment...' %>
    <% end %>
    </div>

【问题讨论】:

  • 能否格式化您的代码
  • 请注意日志中的错误
  • jvillian 只是帮助我做到这一点谢谢我也是在 Stack Over Flow 上发布问题的新手
  • 你并没有真正说出你想要做什么。因此,很难知道如何提供帮助。看起来您正试图在帖子索引页面上显示所有帖子,每个帖子都有一个 cmets 表单。你之前发过这个问题吗?似曾相识。
  • 我没有这是我的第一篇文章...我试图将一篇文章与 cmets 连接我正在关注该应用程序的教程我还检查了 ruby​​ 指南,并且有一个示例可以将文章附加到评论和我的基本结构相同,但我不知道是什么错误

标签: ruby-on-rails comments instagram


【解决方案1】:

在您的index 操作中,您将@post 实例化为ActiveRecord_Relation,此处:

def index
  @post = Post.all
end

然后,您尝试在@postActiveRecord_Relation)上致电comments,这里:

<div class="comment-form">
  <%= form_for([@post, @post.comments.build ], local:true) do |form| %>
    <%= f.text_field :content, placeholder: 'Add a comment...' %>
  <% end %>
</div>

但是,正如错误所说,ActiveRecord_Relation 没有响应 comments

顺便说一句,您不应该为 ActiveRecord_Relation 之类的集合使用单数变量名 (@post)。这很令人困惑,会导致问题。

【讨论】:

  • 好的,但是我需要这样做吗?我是 Rails 新手,我正在关注教程,所以我必须更改“索引操作”或“评论表单”中的某些内容,谢谢您的帮助。 ....
【解决方案2】:

您必须将 accept_nested_attributes_for 添加到您的模型中

class Post < ApplicationRecord
  validates :user_id, presence: true
  belongs_to :user
  has_many :comments, dependent: :destroy
  validates :image, presence: true
  # Here
  accepts_nested_attributes_for :comments
  has_attached_file :image, styles: { :medium => "640x" }
  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/

end

【讨论】:

  • 这将修复undefined method 'comments' for #&lt;Post::ActiveRecord_Relation:0x58ce720&gt;
  • @jvillian 他必须按照你的回答并使用它
  • 我只是添加了“accepts_nested_attributes_for :cmets”,但我仍然有“#<:activerecord_relation:0x5bb8bf8>”的“未定义方法 `cmets'”
【解决方案3】:

我找到了我必须删除“视图”格式上的“@”的解决方案,为什么?我不知道,但它现在可以正常通过代码而没有错误

<div class="comment-form">
<%= form_for([post, post.comments.build ], local:true) do |form| %>
<%= form.text_field :content, placeholder: 'Add a comment...' %>
<% end %>

它可以在没有

的情况下工作
accepts_nested_attributes_for :comments

我这样做是为了让其他人可能比我更容易解决问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多