ActionMailer : NoMethodError in my_class#create undefined method `each' for nil:NilClass

727 views Asked by At

Actionmailer sends out this message when I try to email an index page : "undefined method `each' for nil:NilClass" I think I just need to pass the right argument in my controller, I just can't get my head around this. Please help!?

My inventories_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

Mailer class:

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

mail view :

<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

There are 1 answers

4
shivam On

In Mailer class you are passing @inventory

In mailer view however you are accessing @inventories

If you need a collection of model in your view, you require to create a collection instance variable.

UPDATE:

Mailer class:

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

Mailer View (erb file):

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