加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – ActiveModel :: ForbiddenAttributesError –

发布时间:2020-12-16 19:57:43 所属栏目:百科 来源:网络整理
导读:我有点迷失了我得到了Rails 4错误:ActiveModel :: ForbiddenAttributesError.我明白,这意味着我需要允许项目通过,我已经做了,但我必须缺少一些东西. 评论控制人: class CommentsController ApplicationController def create @post = Post.find(params[:po
我有点迷失了我得到了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)

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读