2017-05-27 29 views
0

模型中藝術家: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 

爲什麼?我不在這裏?謝謝!

+0

是的,平衡你的引號和括號(正如beartech的回答所建議的)。那我們來看看。 –

+0

謝謝你們,但這不是發生了什麼事情。屬性是傳遞給函數調用的參數。我正在檢查(使用==兩行註釋),這些屬性與firstname等哈希值相等(甚至是===)。我不明白的是,爲什麼,如果我實例化一個新的Artist對象,爲什麼它在我傳遞哈希名的時候起作用,但是如果我將屬性變量/參數傳入,它又會起作用,如果我使用屬性變量第三個例子中的塊。 – Daniela

+0

謝謝!有一個「你觀察到的缺失,所以我的狗現在正在pooping, – Daniela

回答

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) 

你爲什麼這樣做,這是不是從你的代碼清晰。

+0

並不確定你想用'#{'在那個代碼中做什麼,除非你試圖插入一些變量,在這種情況下,你是實際上在開始時缺少一個雙引號以匹配額外的一個末尾? – Beartech

+1

第一個'poop'調用(帶有'=='的調用)用於日誌記錄。它顯示'attributes'確實等於哈希 –

+0

感謝球員們,但這不是發生了什麼,屬性是傳遞給函數調用的參數,我正在檢查(你用兩條評論都是==),這些屬性真的是相等的是什麼我不明白爲什麼,如果我實例化一個新的藝術家對象,爲什麼它的作品,如果我通過名字哈希,但不是如果我通過屬性變量/參數在,如果我在塊中使用屬性變量,它又會起作用第三個例子。 – Daniela