Trouble implementing ActiveModel with class attributes on using DelayedJob
I am using Ruby on Rails 3.0.9 and DelayedJob 2.1 and I am trying to implement a "Contact Us" form myself using ActiveModel functionalities. So...
... in my model file I have:
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
... in my view file I have:
<%= 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 %>
... in my router file I have:
match 'contact_us' => 'pages#contact_us', :via => [:get, :post]
... in my controller file I have:
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
All works, except when I use the ::Pages::Mailer.delay.contact_us(@contact_us)
code with the method full_name
related to the full_name
class attribute in the email template (however, it works when in the email template I do not call the full_name
method). That is, when I use the following email template with Dalayed Job I get an 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 %>
When I use the above email template without Dalayed Job (that is, with the ::Pages::Mailer.contact_us(@contact_us).deliver
code) it works.
The related mailer code is:
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
However, if I send a simple email (using the ::Pages::Mailer.delay.contact_us(@contact_us)
) containing a @message_content.inspect
instead of @message_content.full_name
I get the following output (note that the full_name
instance variable exists!) when I receive the email:
#<ContactUs:0x000001013fc378 @full_name="Sample name text", @email="sample@email_provider.com", @subject="Sample subject text", @message="Sample message text", @validation开发者_开发问答_context=nil, @errors={}>
What is the problem with Dalayed Job and how can I solve that?
I really don't understand why this happens since I have full_name
working as-like, for example, the email
attribute for which all works. I also tried to restart my Apache2 server.
Unfortunately, DelayedJob doesn't allow attribute accessors in models. I learned that the hard way, after many, many hours of trying to get it to work. If you create a custom DelayedJob job, you would be able to create attributes specifically for that job's class.
精彩评论