ActionMailer:my_class中的NoMethodError #create undefined方法`每个'为零:NilClass

时间:2014-12-01 07:09:16

标签: ruby-on-rails ruby actionmailer actionview

当我尝试通过电子邮件发送索引页时,Actionmailer会发出此消息: “未定义的方法`每个'为nil:NilClass” 我想我只需要在我的控制器中传递正确的参数,我就是无法理解这一点。请帮忙!?

我的inventoryories_controller:

def create

    @inventory = Inventory.new(inventory_params)
    if @inventory.save
      AppMailer.send_inventory(@inventory).deliver
      redirect_to inventories_path
    else
      render :new
    end
end

梅勒班:

def send_inventory(inventory)
 @inventories = inventory
 mail to: inventory.email, from: "marvin8214@gmail.com", subject: "Last Month Sushi Bar Inventory - ITRBA"
end

邮件视图:

<div class="span5 well">
  <% @inventories.each do |inventory| %>
 <table>
    <tr>
      <td><%= inventory.name %> </td>
      <td><%= inventory.amount %> </td>
      <td><%= inventory.unit %> </td>
    </tr>
  </table>
 <% end %>
</div>

1 个答案:

答案 0 :(得分:0)

在Mailer课程中,您正在传递@inventory

在邮件程序视图中,您正在访问@inventories

如果您需要视图中的模型集合,则需要创建集合实例变量。

<强>更新

梅勒班:

def send_inventory(inventory)
 @inventory = inventory
 mail to: inventory.email, from: "marvin8214@gmail.com", subject: "Last Month Sushi Bar Inventory - ITRBA"
end

Mailer View(erb文件):

Hi,
Your inventory details:    
      <table>
        <tr>
          <td><%= @inventory.name %> </td>
          <td><%= @inventory.amount %> </td>
          <td><%= @inventory.unit %> </td>
        </tr>
      </table>
Thanks