2011-09-21 100 views
2

我正在使用Ruby on Rails 3.1.0和DelayedJob。由於網上有很多人,我得到了「Job failed to load: uninitialized constant Syck::Syck」的錯誤,但我想我至少發現了什麼會導致錯誤(在我的情況下)。我有一個像加載ActiveModel如下:DelayedJob:如何解決「作業加載失敗」的問題?

class Contact 
    include ActiveModel::Conversion 
    include ActiveModel::Validations 
    include ActiveModel::Dirty 
    extend ActiveModel::Naming 
    extend ActiveModel::Translation 

    attr_accessor :full_name, :email, :subject, :message 

    def initialize(attributes = {}) 
    attributes.keys.each do |attr| 
     instance_variable_set "@" + attr.to_s, attributes[attr.to_sym] 
    end 
    end 

    validates_presence_of :full_name, :email, :subject, :message  

    def persist 
    @persisted = true 
    end 

    def persisted? 
    false 
    end 
end 

相關的控制器操作是:

def contact 
    @contact = Contact.new(params[:contact]) 

    if @contact.valid? 
    ::Contact::Mailer.delay.contact(@contact) 

    respond_to do |format| 
     format.html { redirect_to root_path } 
    end 
    else 
    respond_to do |format| 
     format.html { render :action => :contact } 
    end 
    end 
end 

我注意到,我的問題「著名的」 \「臭名昭著」 Job failed to load: uninitialized constant Syck::Syck只發生,如果我跑了​​。如果我重新實現這樣上面的控制器動作:

def contact 
    @contact = Contact.new(params[:contact]) 

    ::Contact::Mailer.delay.contact(@contact) 

    respond_to do |format| 
    format.html { redirect_to root_path } 
    end 
end 

預期所有的工作:我沒有得到錯誤和電子郵件成功發送。 幾句話,當我在控制器動作內運行​​(我可以在沒有使用if ... else語句的情況下運行它)它會生成Job failed to load錯誤。我真的不明白這個與DelayedJob gem和valid?方法有關的奇怪行爲。

爲什麼會發生?我該如何解決這個問題?在DelayedJob: 「Job failed to load: uninitialized constant Syck::Syck」



更多信息最新通報

如果我在這兩種情況下,使用@contact.errors或者是不使用​​方法...

調試

......當我使用​​方法(DelayedJob不工作),我得到

#<ActiveModel::Errors:0x00000101759408 @base=#<Contact:0x000001017597f0 @full_name="Sample name", @email="[email protected]", @subject="Sample subject", @message="Sample message content.", @validation_context=nil, @errors=#<ActiveModel::Errors:0x00000101759408 ...>>, @messages={}> 

......當使用​​方法(DelayedJob作品),我得到

#<ActiveModel::Errors:0x00000101759408 @base=#<Contact:0x000001017597f0 @full_name="Sample name", @email="[email protected]", @subject="Sample subject", @message="Sample message content.", @errors=#<ActiveModel::Errors:0x00000101759408 ...>>, @messages={}> 

。注意,在第二種情況下,@validation_context=nil不存在,並且,在這兩種情況下,有一個「嵌套」 <ActiveModel::Errors:0x0000010175940 ...> ST atement。這是一個錯誤?

+0

我遇到了同樣的問題。你找到了解決方案嗎? – Bastien

回答

0

我找到了適用於我的解決方案。您可以在Contact類中重新定義'Object#to_yaml_properties'方法,以僅包含您需要的屬性。從而排除'錯誤'變量。

def to_yaml_properties 
    ['@full_name', '@email', '@subject', '@message'] 
end 

希望這會有所幫助。

相關問題