【问题标题】:Ruby on rails - unable to create comments. NoMethodErrorRuby on rails - 无法创建评论。无方法错误
【发布时间】:2018-04-27 22:11:27
【问题描述】:

我正在创建我的第一个 RoR 应用程序 - 一个论坛。我正在尝试将 cmets 添加到论坛,但我遇到了一些错误。我在谷歌上搜索过类似的问题,但似乎都没有解决我的问题。这是我的代码:

评论控制器:

class CommentsController < ApplicationController
  def create
    @forum = Forum.find(params[:forum_id])

    if !@forum.nil?
        puts "Forum object is not nil"
    end

    @comment = @forum.comment.create(comment_params)

    redirect_to forum_path
  end

  private

  def comment_params
    params.require(:comment).permit(:body)
  end
end

论坛控制器是自动生成的,我没有改进去。(使用rails生成脚手架论坛,如果你还想看,请告诉我)

class Comment < ApplicationRecord
  belongs_to :forum
end

class Forum < ApplicationRecord
  has_many :comments
  validates :title, presence: true,
  length: {maximum: 50}
  validates :body, presence: true
end

下面是show.html.erb 论坛页面

的部分表格
<h2>Comments</h2>

<% @forum.comments.each do |comment| %>
<p>
    <%= comment.body %>
</p>

<% end %>

<h2>Add a comment</h2>

<%= form_for([@forum, @forum.comments.build]) do |f| %>
<p>
    <%= f.label :body %><br/>
    <%= f.text_area :body %>
</p>
<p>
    <%= f.submit %>
</p>
<% end %>

这是来自 rails 的错误:

undefined method `comment' for #<Forum:0x444d518>

摘录如下:

end
 @comment = @forum.comment.create(comment_params) #highlighted
 redirect_to forum_path
end

【问题讨论】:

  • 1) Rails 3 已经过时了。 2)请格式化您的帖子。
  • 我同意 coreward,如果这是一个新应用程序,我建议使用 Rails 5。如果您想要稍旧的版本,请使用最新的 Rails 4。顺便说一句,您的模型类的事实继承自 ApplicationRecord 让我相信你正在使用 Rails 5。

标签: ruby-on-rails ruby comments ruby-on-rails-5


【解决方案1】:

您的论坛模型中似乎缺少has_many :comments 关联。看看吧。

或者,如果 has_many 关联退出,则它允许您在任何论坛对象上调用“cmets”,而不是“comment”。

如果您想为特定论坛创建评论,您可以这样做:

@comment = Comment.create(comment_params) #create a comment associated with this forum.

【讨论】:

  • 它有这个。我检查了
  • 按照您的建议执行此错误:参数数量错误(0..1 为 2)
  • 抱歉耽搁了,有点忙。让我知道编辑后的版本是否有效。基本上只是将强大的 comment_params 传递给 create 方法。
【解决方案2】:

问题在于您的 CommentsController 中的这一行 @comment = @forum.comment.create(comment_params)。 它应该是 @comment = @forum.comments.create(comment_params)comments 应该是复数。

你的代码应该是:

class CommentsController < ApplicationController
  def create
    @forum = Forum.find(params[:forum_id])
    @comment = @forum.comments.create(comment_params)
    redirect_to forum_path
  end

  private

  def comment_params
    params.require(:comment).permit(:body)
  end
end

【讨论】:

    【解决方案3】:

    确保您在Forum 模型中有comments 关联:

    class Forum < ApplicationRecord
      has_many :comments
    end
    

    【讨论】:

      【解决方案4】:

      所以,问题出在 CommentsController 上。

      我更改了 cmets 控制器来执行此操作:

      def create
              @forum = Forum.find(params[:forum_id])
      
              @comment = Comment.new(comment_params)
              @comment.forum_id = @forum.id
              @comment.save!
      
              redirect_to forum_path(@forum)
          end
      
      
          private
              def comment_params
                  params.require(:comment).permit(:body)
              end
      

      似乎成功了。

      感谢所有给出答案的人。

      【讨论】:

      • 有更好的方法来做到这一点。请检查我的答案。
      • 宁可使用@comment = @forum.comments.new(comment_params)@comment.save
      猜你喜欢
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多