【问题标题】:Trouble implementing ActiveModel with class attributes on using DelayedJob在使用 DelayedJob 时使用类属性实现 ActiveModel 时遇到问题
【发布时间】:2011-08-09 07:11:05
【问题描述】:

我正在使用 Ruby on Rails 3.0.9 和 DelayedJob 2.1,并且我正在尝试使用 ActiveModel 功能自己实现“联系我们”表单。所以...

...在我的模型文件中,我有:

class ContactUs
  include ActiveModel::Conversion
  include ActiveModel::Validations


  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 :full_name,
    :presence   => true

  validates :email,
    :presence   => true

  validates :subject,
    :presence   => true

  validates :message,
    :presence   => true


  def persist
    @persisted = true
  end

  def persisted?
    false
  end
end

...在我的视图文件中,我有:

<%= form_for @contact_us, :url => contact_us_path do |f| %>
  <%= f.text_field :full_name %>
  <%= f.text_field :email %>
  <%= f.text_field :subject %>
  <%= f.text_area  :message %>
<% end %>

...在我的路由器文件中:

match 'contact_us' => 'pages#contact_us', :via => [:get, :post]

...在我的控制器文件中,我有:

class PagesController < ApplicationController
  def contact_us
    case request.request_method

    when 'GET'
      @contact_us = ContactUs.new

    when 'POST'
      @contact_us = ContactUs.new(params[:contact_us])

      # ::Pages::Mailer.delay.contact_us(@contact_us) # If I use this code, I will get an error with the 'full_name' attribute (read below for more information)
      ::Pages::Mailer.contact_us(@contact_us).deliver # If I use this code, it will work
    end
  end
end

除了我使用::Pages::Mailer.delay.contact_us(@contact_us) 代码和与电子邮件模板中的full_name 类属性相关的方法full_name 时,所有工作都有效(但是,它在电子邮件模板中有效)我确实调用full_name 方法)。也就是说,当我将以下电子邮件模板与 Dalayed Job 一起使用时,我会收到 undefined method 'full_name\' for #&lt;ContactUs:0x000001041638c0&gt; \n/RAILS_ROOT/app/views/pages/mailer/contact_us.html.erb

MESSAGE CONTENT:
<br /><br />

<%= @message_content.full_name %> # If I comment out this line it will work.
<br />
<%= @message_content.email %>
<br />
<%= @message_content.subject %>
<br />
<%= @message_content.message %>

当我使用上述电子邮件模板没有 Dalayed Job(即使用::Pages::Mailer.contact_us(@contact_us).deliver 代码)时,它可以工作。

相关邮件代码为:

class Pages::Mailer < ActionMailer::Base
  default_url_options[:host] = <my_web_site_URL>
  default :from => "<my_email_address@provaider_name.com"

  def contact_us(message_content)
    @message_content = message_content

    mail(
      :to      => <my_email_address@provaider_name.com>,
      :subject => "Contact us"
    ) do |format|
      format.html
    end
  end
end

但是,如果我发送一封包含@message_content.inspect 而不是@message_content.full_name 的简单电子邮件(使用::Pages::Mailer.delay.contact_us(@contact_us)),当我收到电子邮件时,我会得到以下输出(注意full_name 实例变量存在!) :

#<ContactUs:0x000001013fc378 @full_name="Sample name text", @email="sample@email_provider.com", @subject="Sample subject text", @message="Sample message text", @validation_context=nil, @errors={}> 

Dalayed Job 有什么问题,我该如何解决?


我真的不明白为什么会发生这种情况,因为我有 full_name 一样工作,例如,所有工作的 email 属性。我还尝试重新启动我的 Apache2 服务器。

【问题讨论】:

  • 如果您将全名重命名为没有 _ 的其他名称(全名)怎么办?我不确定,但是当它序列化/反序列化 @contact_us 对象时,延迟作业可能存在错误。
  • 在使用 full_name 更改模型时,您是否太正确地重新启动了 delay_job 工作人员?

标签: ruby-on-rails ruby-on-rails-3 attributes delayed-job activemodel


【解决方案1】:

不幸的是,DelayedJob 不允许模型中的属性访问器。经过很多很多小时的努力让它发挥作用,我才学会了这一点。如果您创建自定义 DelayedJob 作业,您将能够专门为该作业的类创建属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    • 2022-12-18
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多