2015-10-05 76 views
-3

客戶端最多可以上傳三個文件。我想根據他們選擇的描述來設置文件的狀態。上傳工作正常,並且靜態狀態正常,但動態會引發錯誤。include和if語句裏面的每個

def build_document_objects 
    [:first, :second, :third].each do |doc| 
    d = "#{doc}_document" 
    if self.send("#{d}_type") == "this Type" 
     doc_status = 'one' 
    else 
     doc_status = 'two' 
     self.send("#{d}=", user.documents.new(
     description: "Foo", 
     file: self.send("#{d}_file"), 
     document_type: self.send("#{d}_type"), 
     status: doc_status 
    )) 
    end 
    end 
end 

當我運行它,我得到以下異常:

undefined method `save'' for nil:NilClass')) 

如果我這樣做:

def build_document_objects 
[:first, :second, :third].each do |doc| 
    # "first_document" 
    d = "#{doc}_document" 
    if self.send("#{d}_type") == "this Type" 
    doc_status = 'one' 
else 
    doc_status = 'two' 
    end # change where the IF ends 
    self.send("#{d}=", user.documents.new(
    description: "Foo", 
    file: self.send("#{d}_file"), 
    document_type: self.send("#{d}_type"), 
    status: doc_status 
    )) 
end 
end 

如果該文件描述不this type,該記錄將被保存。然而,與:

if self.send("#{d}_type") == "this Type" 

我得到的例外。由於沒有狀態,因此記錄不會被保存。

+1

顯示您的錯誤 –

+0

錯誤是記錄不會保存 – techguy

+0

...特別是,異常消息和它發生的行。請編輯將該信息添加到問題中,而不是在評論中詳細說明。 –

回答

0

看來我堅果

def build_document_objects 
[:first, :second, :third].each do |doc| 
# "first_document" 
d = "#{doc}_document" 
if self.send("#{d}_type") == "this Type" 
doc_status = 'one' 
else 
doc_status = 'two' 
end # change where the IF ends 

self.send("#{d}=", user.documents.new(

description: "Foo", 
file: self.send("#{d}_file"), 
document_type: self.send("#{d}_type"), 

status: doc_status 

)) 

end 

end 

工作正常 如果只是需要在方法得當。