Ruby   发布时间:2019-10-05  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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

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

谢谢!

@H_673_14@

解决方法

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

你要

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

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

@H_673_14@ @H_673_14@

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – ActiveModel :: ForbiddenAttributesError – Rails 4全部内容,希望文章能够帮你解决ruby-on-rails – ActiveModel :: ForbiddenAttributesError – Rails 4所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。