ruby-on-rails – ActiveModel :: ForbiddenAttributesError – Rails 4

我有点迷失了我得到了Rails 4错误:ActiveModel :: ForbiddenAttributesError.我明白,这意味着我需要允许项目通过,我已经做了,但我必须缺少一些东西.

评论控制人:

class CommentsController < ApplicationController
    def create
       @post = Post.find(params[:post_id])
       @comment = @post.comments.create!(params[:comment])
       redirect_to @post
    end

    private
        # Never trust parameters from the scary internet,only allow the white list through.
        def comment_params
            params.require(:comment).permit(:post_id,:comment,:body)
        end

end

创建注释迁移

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.references :post
      t.text :body

      t.timestamps
    end
  end

  def self.down
    drop_table :comments
  end  
end

在这里缺少什么?如果您需要查看任何其他代码,请告诉我们.

谢谢!

解决方法

代替
@comment = @post.comments.create!(params[:comment])

你要

@comment = @post.comments.create!(comment_params)

你做了所有辛苦的工作,没有使用允许的属性

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...