ActionMailer 失败

发布于 2024-12-11 16:37:56 字数 1230 浏览 0 评论 0原文

发送电子邮件时,我收到无方法错误

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮子 2024-12-18 16:37:57

您的邮件程序想要一个 Order 作为其参数:

class OrdersMailer < ActionMailer::Base
  def email_copy_of_order_to_client(order)
    #...

但您给它一个 OrderController 实例:

class OrdersController < InheritedResources::Base
  def deliver
    @email = OrdersMailer.email_copy_of_order_to_client(self).deliver
    #...

self 是控制器的实例,而不是 Order 的实例。因此,您关于 OrdersController 的错误消息:

undefined method `client' for #<OrdersController:0x007fdc22aa4938>

您可能希望在 deliver 中出现类似的内容:

def deliver
  order = Order.find(params[:id])
  # check access/ownership/... on order
  @email = OrdersMailer.email_copy_of_order_to_client(order).deliver
  #...

Your mailer wants an Order as its argument:

class OrdersMailer < ActionMailer::Base
  def email_copy_of_order_to_client(order)
    #...

but you're giving it an instance of OrderController:

class OrdersController < InheritedResources::Base
  def deliver
    @email = OrdersMailer.email_copy_of_order_to_client(self).deliver
    #...

That self is an instance of the controller, not an instance of Order. Hence your error message about OrdersController:

undefined method `client' for #<OrdersController:0x007fdc22aa4938>

You probably want something like this in your deliver:

def deliver
  order = Order.find(params[:id])
  # check access/ownership/... on order
  @email = OrdersMailer.email_copy_of_order_to_client(order).deliver
  #...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文