DelayedJob:如何解决问题“作业加载失败”? [英] DelayedJob: How to solve the problem "Job failed to load"?

查看:498
本文介绍了DelayedJob:如何解决问题“作业加载失败”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Ruby on Rails 3.1.0和DelayedJob。正如网路上的许多人,我得到作业无法加载:未初始化的常量Syck :: Syck 错误,但我认为我发现至少产生错误(在我的情况)。我有一个ActiveModel,如下所示:

  class Contact 
include ActiveModel :: Conversion
include ActiveModel: :验证
包括ActiveModel :: Dirty
扩展ActiveModel ::命名
扩展ActiveModel :: Translation

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

def initialize(attributes = {})
attributes.keys.each do | attr |
instance_variable_set@+ attr.to_s,属性[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])

如果@ contact.valid?
::联系人:: 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

我注意到,我的着名的\\臭名昭着的作业的问题无法加载:未初始化的常量Syck :: Syck 只有在我运行 @ contact.valid? 的。如果我重新执行上述控制器操作,如下所示:

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

::联系人:: Mailer.delay.contact(@contact)

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

所有工作正如预期的:我没有得到错误,并且电子邮件成功发送。 几句话,当我在控制器动作中运行 @ contact.valid?(我也可以运行,而不使用 if ... else 语句)它会生成作业无法加载错误。我真的不明白这个与DelayedJob gem和有关的奇怪行为。方法。



为什么会发生如何解决问题?






更多信息 DelayedJob:作业无法加载:未初始化的常量Syck :: Syck






更新



如果我使用或不使用 @ contact.valid?在案例中调试 @ contact.errors 方法...



...当我使用 @ contact.valid?方法(DelayedJob不起作用)我得到

 #< ActiveModel :: Errors:0x00000101759408 @ base = < Contact:0x000001017597f0 @ full_name =Sample name,@ email =foo@bar.com,@ subject =Sample subject,@ message =Sample message content。,@ validation_context = nil,@ errors = #< ActiveModel :: Errors:0x00000101759408 ...>>> @messages = {}> 

...当不要使用 @ contact.valid?方法(DelayedJob工作)我得到

 #< ActiveModel ::错误:0x00000101759408 @base =#<联系人:0x000001017597f0 @ full_name =样本名称,@ email =foo@bar.com,@ subject =示例主题,@ message =示例消息内容,@errors =#< ActiveModel :: Errors:0x00000101759408 ...>> @messages = {}> 

请注意,在第二种情况下, @ validation_context = nil 不存在,并且在这两种情况下都存在嵌套< ActiveModel :: Errors:0x0000010175940 ...> 语句。 em> 这是一个错误?

解决方案

我找到了一个适用于我的解决方案。您可以重新定义对象# to_yaml_properties'方法,只包括您需要的属性。因此排除错误变量。

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

希望这有帮助。 p>

I am using Ruby on Rails 3.1.0 and DelayedJob. As many people on the web I get the "Job failed to load: uninitialized constant Syck::Syck" error, but I think I discovered at least what generates the error (in my case). I have an ActiveModel like the following:

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

The related controller action is:

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

I noted that my problem with the "famous"\"infamous" Job failed to load: uninitialized constant Syck::Syck happens only if I run the @contact.valid?. If I re-implement the above controller action like this:

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

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

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

all work as expected: I don't get the error and the e-mail is successfully sent. In few words, when I run @contact.valid? inside the controller action (I can run that also without using the if ... else statement) it generates the Job failed to load error. I really do not understand this strange behavior related to the DelayedJob gem and the valid? method.

Why it happens? How can I solve the problem?


More info at DelayedJob: "Job failed to load: uninitialized constant Syck::Syck"


UPDATES

If I debug the @contact.errors in both cases using or not using the @contact.valid? method...

... when I use the @contact.valid? method (DelayedJob does not work) I get

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

... when I do not use the @contact.valid? method (DelayedJob works) I get

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

Note that in the second case the @validation_context=nil is not present and that in both cases there is a "nested" <ActiveModel::Errors:0x0000010175940 ...> statement. Is that a bug?

解决方案

I found a solution that works for me. You can redefine the 'Object#to_yaml_properties' method within your Contact class to only include the properties you need. And thus exclude the 'errors' variable.

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

Hope this helps.

这篇关于DelayedJob:如何解决问题“作业加载失败”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆