Reputation: 3216
I'm building a marketplace app where sellers can list items to sell. I've set up ActionMailer to send out an order confirmation email which is triggered on order.save
. I'm now trying to set up a shipping confirmation email, which will be triggered when the seller clicks on a button to indicate the item has been shipped.
I'm getting stuck on the data being passed through to the ActionMailer. I get an undefined method 'buyer'
error on @order.buyer.email
in the mailer method below. How do I pass buyer info to the mailer?
I'd also like to pass listing data to the mailer so I can use listing details in the email content.
Model info: I have a User model, a Listing model and an Order model. The order model has a buyer_id column which is a foreign key to the User model and a listing_id which is a foriegn key to the listing model. The users email is stored in the User model.
Here is my code:
#route
get 'shipconf' => "orders#shipconf"
#order_controller
def shipconf
AutoNotifier.shipconf_email(@order).deliver
end
#AutoNotifier.rb
def shipconf_email(order)
@order = order
mail(to: @order.buyer.email, bcc: "[email protected]", subject: 'You have a new order at Outfit Additions.')
end
#sellers admin view table with order info. seller clicks button to send mail.
<% @orders.each do |order| %>
<tr>
<td><%= order.id %></td>
<td><%= link_to order.listing.name.downcase.titleize, order.listing, data: { no_turbolink: true } %></td>
<td><%= number_to_currency(order.listing.price) %></td>
<td><%= order.shipaddress %></td>
<td><%= best_in_place order, :tracking, :type => :input, :url => listing_order_path(id: order.id, listing_id: order.listing_id) %> </td>
<td><%= link_to "Send Email", shipconf_path(order), class: "btn btn-sm btn-primary" %></td>
</tr>
<% end %>
#order model
belongs_to :listing
belongs_to :buyer, class_name: "User"
belongs_to :seller, class_name: "User"
#user model
has_many :listings, dependent: :destroy
has_many :orders, class_name: "Order", foreign_key: "seller_id"
has_many :purchases, class_name: "Order", foreign_key: "buyer_id"
#listing model
belongs_to :user
has_many :orders
Upvotes: 0
Views: 742
Reputation: 2662
Did you define the @order
variable somewhere for your shipconf action?
If not you have to define it, for example like this:
#order_controller
def shipconf
@order = Order.find(params[:id])
AutoNotifier.shipconf_email(@order).deliver
end
And change your link to:
shipconf_path(id: order.id)
Upvotes: 1