【问题标题】:Using Struct in ActionMailer preview在 ActionMailer 预览中使用 Struct
【发布时间】:2016-04-14 06:35:02
【问题描述】:

我正在使用 ActionMailer Preview 测试我的一个邮件程序,并正在使用 Struct 创建我的对象

class TransactionMailerPreview < ActionMailer::Preview
  include Roadie::Rails::Automatic

  def transaction_complete_mailer
    orders = Struct.new(:image, :image_size, :mount, :frame, :frame_color)
    transaction = Struct.new(:first_name, :email)
    @transaction = transaction.new('Richard Lewis', 'test@gmail.com')
    @orders = orders.new("Test Print Name", "10x8 Image Size", "10x8 Mount Size", "10x8 Frame Size", "White Frame")
    TransactionMailer.transaction_complete_mailer(@transaction, @orders) 
  end
end

这是我真正的邮件类

class TransactionMailer < ApplicationMailer
  default from: ENV['EMAIL_ADDRESS']

  def transaction_complete_mailer(transaction, orders)
    @transaction = transaction
    @orders = orders
    attachments.inline['photography-logo-text.png'] = File.read(Rails.root.join('app', 'assets', 'images', 'photography-logo-text.png'))
    mail(to: @transaction.email, subject: 'Your order from Glenn GB Photography') do |format|
      format.html { render file: 'transaction_mailer/transaction_message.html.erb', layout: 'mailer' }
    end
  end
end

在我看来,我会遍历@orders,因为可能有多个

transaction_message.htmnl.erb

<% @orders.each do |order| %>
  <div class="order-summary">
    <p><span class="bold">Print:</span><%= order.image %></p>
    <p><span class="bold">Print Size:</span><%= order.image_size %></p>
    <p><span class="bold">Mount:</span><%= order.mount %></p>
    <p><span class="bold">Frame:</span><%= order.frame %></p>
    <p><span class="bold">Frame Color:</span><%= order.frame_color %></p>
  </div>
<% end %>

当我在预览中操作此邮件时,我收到一个错误

undefined method `image' for "Test Print Name":String

这里有两个问题

  1. 为什么会出现错误?

  2. 如何创建多个订单对象?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 actionmailer


    【解决方案1】:

    @ordersstruct 类型的单数对象,但您的 transaction_complete_mailer 需要一个名为 @orders 的数组/集合。因此,当您在 Mailer 中调用 @orders.each do |order| 时,它实际上是在遍历 @orders 结构对象中的每个键/值。这解释了您遇到的错误,因为 "Test Print Name" 是您的 Struct 中声明的第一个键。

    将结构包装在数组中应该可以解决问题:

    class TransactionMailerPreview < ActionMailer::Preview
      include Roadie::Rails::Automatic
    
      def transaction_complete_mailer
        orders = Struct.new(:image, :image_size, :mount, :frame, :frame_color)
        transaction = Struct.new(:first_name, :email)
        @transaction = transaction.new('Richard Lewis', 'test@gmail.com')
        @order = orders.new("Test Print Name", "10x8 Image Size", "10x8 Mount Size", "10x8 Frame Size", "White Frame") # Renamed to @order, since this is a single object
        TransactionMailer.transaction_complete_mailer([@transaction], [@order]) 
      end
    end
    

    【讨论】:

    • 啊,谢谢,这很有意义 :-) 你知道我将如何使用 struct 创建多个记录吗?
    • 一个结构体,基本上是一个 Hash 对象,其行为类似于传统的 Ruby 对象,它旨在表示单个对象而不是集合。你可以让结构的字段是一个数组,但这会破坏目的。
    • 好的,谢谢,将使用替代方法,不过,谢谢,至少为我清理了一些东西
    • 没问题,祝你好运。
    猜你喜欢
    • 2022-09-23
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多