2

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_namefull_nameします(ただし、メールテンプレートでメソッドを呼び出さないfull_name場合は機能します)。つまり、Dalayed Job で次の電子メール テンプレートを使用すると、次のようになりますundefined method 'full_name\' for #<ContactUs:0x000001041638c0> \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

ただし、代わりにを::Pages::Mailer.delay.contact_us(@contact_us)含む単純なメールを ( を使用して) 送信すると、メールを受信したときに次の出力が得られます (インスタンス変数が存在することに注意してください!)。@message_content.inspect@message_content.full_namefull_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 サーバーを再起動しようとしました。

4

1 に答える 1

0

残念ながら、DelayedJob では、モデルで属性アクセサーを使用できません。それを機能させるために何時間も努力した後、私はそれが難しい方法であることを学びました. カスタム DelayedJob ジョブを作成すると、そのジョブのクラス専用の属性を作成できます。

于 2013-07-19T06:21:37.730 に答える