rails 4 ActiveModel :: ForbiddenAttributesError属性作为变量失败,属性拼写成功

时间:2017-05-27 14:41:45

标签: ruby-on-rails-4 activerecord

模特艺术家:

编辑以反映评论中的误解。

Dog.poop只是我使用colorize来获得更明显的put输出的愚蠢方式。它只吃字符串,这就是为什么Dog.poop("#{}")

Dog.poop(#{attributes == {" firstname" =>" Firstname"," lastname" =>" Lastname&# 34;,"说明" =>"测试"," document_id" =>"","电子邮件&#34 ; =>"","移动" =>""," url" =>"&# 34;,"独奏者" =>" 1"," performance_order" =>" 1"}}"

只是为了显示我检查属性是否确实==使用{" firstname等哈希。是真的(===也是如此)

因为以下内容

示例1:尝试创建一个新的Artist对象,从函数参数中提供变量属性。失败。

def self.a_new_one(attributes)
    Dog.poop(#{attributes == {"firstname"=>"Firstname", "lastname"=>"Lastname", "description"=>"test", "document_id"=>"", "email"=>"", "mobile"=>"", "url"=>"", "soloist"=>"1", "performance_order"=>"1"}}"
    # --> true

    Dog.poop("#{self.new(attributes)}")
    # --> ActiveModel::ForbiddenAttributesError
end

BUT:

示例2:尝试创建一个新的Artist对象,输入{" firstname等哈希。成功。

这很奇怪,如果属性== {{" firstname等Hash是真的!

def self.a_new_one(attributes)
    Dog.poop(#{attributes == {"firstname"=>"Firstname", "lastname"=>"Lastname", "description"=>"test", "document_id"=>"", "email"=>"", "mobile"=>"", "url"=>"", "soloist"=>"1", "performance_order"=>"1"}}"
    # --> true

    Dog.poop("#{self.new({"firstname"=>"Firstname", "lastname"=>"Lastname", "description"=>"test", "document_id"=>"", "email"=>"", "mobile"=>"", "url"=>"", "soloist"=>"1", "performance_order"=>"1"})}")
    # --> #<Artist:0x007fe39ee2cc08>
end

这也有效:

示例3:如果我通过将属性传递到块中并逐个添加它来创建对象,那么:成功。

为什么?为什么在ex 1中失败,但在ex2和ex3中成功?

def self.a_new_one(attributes)
    Dog.poop(#{attributes == {"firstname"=>"Firstname", "lastname"=>"Lastname", "description"=>"test", "document_id"=>"", "email"=>"", "mobile"=>"", "url"=>"", "soloist"=>"1", "performance_order"=>"1"}}"
    # --> true

    self.new do |artist|
        attributes.each do |key, value|
            artist[key] = value
        end
        Dog.poop("#{artist}")
        # --> #<Artist:0x007fe39ee2cc08>
    end
end

为什么?我没有到这里来的是什么?谢谢!

1 个答案:

答案 0 :(得分:1)

看起来您正在尝试使用==进行变量赋值,然后使用变量插值将它们放入对象中。

Dog.poop(#{attributes == {"firstname"=>"Firstname", "lastname"=>"Lastname", "description"=>"test", "document_id"=>"", "email"=>"", "mobile"=>"", "url"=>"", "soloist"=>"1", "performance_order"=>"1"}}" 

您在上述行的末尾也遗漏了),并在最后加了一个双引号。

我认为你真正要做的是:

    Dog.poop("#{attributes = {"firstname"=>"Firstname", "lastname"=>"Lastname", "description"=>"test", "document_id"=>"", "email"=>"", "mobile"=>"", "url"=>"", "soloist"=>"1", "performance_order"=>"1"}}")

$> attributes
     => {"firstname"=>"Firstname",
     "lastname"=>"Lastname",
     "description"=>"test",
     "document_id"=>"",
     "email"=>"",
     "mobile"=>"",
     "url"=>"",
     "soloist"=>"1",
     "performance_order"=>"1"}

仍然不确定你为什么这样做,对我来说没有意义,但也许你可以解释一下。如果将attributes传递给方法,那么为什么要覆盖它呢?

或者您是否正在检查传入的attributes是否符合您的预期,然后保存?在这种情况下,我认为这只是"之前开始时遗失#{attributes...的情况。

Dog.poop("#{attributes == {"firstname"=>"Firstname", "lastname"=>"Lastname", "description"=>"test", "document_id"=>"", "email"=>"", "mobile"=>"", "url"=>"", "soloist"=>"1", "performance_order"=>"1"}}")

基本上只是:

Dog.poop(true)

你为什么要这样做,你的代码并不清楚。