模型中藝術家:rails 4 ActiveModel :: ForbiddenAttributesError屬性作爲變量失敗,屬性拼寫成功
編輯以反映評論中的誤解。
Dog.poop是我使用colorize獲得更多可見放置輸出的愚蠢方式。它只吃字符串,這就是爲什麼Dog.poop(「#{}」)
Dog.poop(#{attributes == {「firstname」=>「Firstname」,「lastname」=>「Lastname」, 「description」=>「test」,「document_id」=>「」,「email」=>「」,「mobile」=>「」,「url」=>「」,「soloist」=>「1」, 「performance_order」 =>「1」}}「
只是說明我檢查,如果屬性確實==與{」名字等散。是真的(===是真的太)
因爲以下內容
示例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:試圖使一個新的藝術家目的,在{「姓名等哈希餵養。成功。
這是奇怪的,如果屬性== {{「名字等哈希是真的
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:如果我做對象通過傳遞到屬性塊和添加他們一個接一個,然後又:成功
WHY爲什麼失敗的前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
?
爲什麼?我不在這裏?謝謝!
是的,平衡你的引號和括號(正如beartech的回答所建議的)。那我們來看看。 –
謝謝你們,但這不是發生了什麼事情。屬性是傳遞給函數調用的參數。我正在檢查(使用==兩行註釋),這些屬性與firstname等哈希值相等(甚至是===)。我不明白的是,爲什麼,如果我實例化一個新的Artist對象,爲什麼它在我傳遞哈希名的時候起作用,但是如果我將屬性變量/參數傳入,它又會起作用,如果我使用屬性變量第三個例子中的塊。 – Daniela
謝謝!有一個「你觀察到的缺失,所以我的狗現在正在pooping, – Daniela