2015-10-05 63 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 

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