ForbiddenAttributesError

ForbiddenAttributesError

本文介绍了ActiveModel::ForbiddenAttributesError - Rails 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点失落.我收到 Rails 4 错误:ActiveModel::ForbiddenAttributesError.我明白这意味着我需要允许项目通过,我已经完成了,但我一定遗漏了一些东西.

I'm a bit lost. I an getting the Rails 4 error: ActiveModel::ForbiddenAttributesError. I understand that this means I need to permit items to pass, which I have done, but I must be missing something.

评论控制器:

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

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

What am I missing here? Let me know if you need to see any other code.

谢谢!

推荐答案

代替

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

你想要

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

您在没有使用允许的属性的情况下完成了所有艰苦的工作!

You did all the hard work without using the permitted attributes!

这篇关于ActiveModel::ForbiddenAttributesError - Rails 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 11:55