ActionMailer 失败
发送电子邮件时,我收到无方法错误
NoMethodError in OrdersController#deliver
undefined method `client' for #<OrdersController:0x007fdc22aa4938>
当尝试使用 ActionMailer My Mailer
class OrdersMailer < ActionMailer::Base
def email_copy_of_order_to_client(order)
@order = order
@client = @order.client
mail(:to => @client.email, :subject => "Your order")
end
当然通过控制台我得到了我想要的内容
Order.last.client
Order Load (0.2ms) SELECT "orders".* FROM "orders" ORDER BY "orders"."id" DESC LIMIT 1
=> #<Client id: 3, name: "Test Client", address1: "", address2: "", city: ""...`
已编辑
class OrdersController < InheritedResources::Base
def deliver
@email = OrdersMailer.email_copy_of_order_to_client(self).deliver
respond_to do |format|
if @email.valid?
format.html { redirect_to order, notice: 'Order was successfully sent.' }
else
format.html { render action: "show", alert: 'An error occured. Please try again.' }
end
end
end
Order#show view
<%= pill_button_link_to "Send Order by Email", deliver_order_path(@order) %>
有什么想法吗?
I get a no method error when trying to send out email with ActionMailer
NoMethodError in OrdersController#deliver
undefined method `client' for #<OrdersController:0x007fdc22aa4938>
My Mailer
class OrdersMailer < ActionMailer::Base
def email_copy_of_order_to_client(order)
@order = order
@client = @order.client
mail(:to => @client.email, :subject => "Your order")
end
Of course through the console I get what I want
Order.last.client
Order Load (0.2ms) SELECT "orders".* FROM "orders" ORDER BY "orders"."id" DESC LIMIT 1
=> #<Client id: 3, name: "Test Client", address1: "", address2: "", city: ""...`
EDITED
class OrdersController < InheritedResources::Base
def deliver
@email = OrdersMailer.email_copy_of_order_to_client(self).deliver
respond_to do |format|
if @email.valid?
format.html { redirect_to order, notice: 'Order was successfully sent.' }
else
format.html { render action: "show", alert: 'An error occured. Please try again.' }
end
end
end
Order#show view
<%= pill_button_link_to "Send Order by Email", deliver_order_path(@order) %>
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的邮件程序想要一个 Order 作为其参数:
但您给它一个 OrderController 实例:
该
self
是控制器的实例,而不是 Order 的实例。因此,您关于 OrdersController 的错误消息:您可能希望在
deliver
中出现类似的内容:Your mailer wants an Order as its argument:
but you're giving it an instance of OrderController:
That
self
is an instance of the controller, not an instance of Order. Hence your error message about OrdersController:You probably want something like this in your
deliver
: