使用 DelayedJob 时使用类属性实现 ActiveModel 时遇到问题
我正在使用 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 中使用以下电子邮件模板时,我得到了 #
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_name
当我收到电子邮件时,我得到以下输出(请注意,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< /code> 属性,所有这些都有效。我还尝试重新启动我的 Apache2 服务器。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,DelayedJob 不允许在模型中使用属性访问器。经过很多很多个小时的尝试让它发挥作用之后,我才了解到这一点。如果您创建自定义 DelayedJob 作业,您将能够专门为该作业的类创建属性。
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.