简体   繁体   中英

How to call a mailer action in a controller from a link_to post

I'm trying to call a mailer I have in the create action in my interests controller:

  def create
       if @interest.save
            UserMailer.interest_to_seller(@interest).deliver_now
  etc....

This works great when I'm going to the CRUD /interest/new form and creating it there, but I'm actually trying to call this from a post method in a different place. It's as if the create actions is being skipped all together, but that's impossible because the record is being created, right?

My routes.rb:

 resources :seller_listings do
     post :add_interest, on: :member
  end

And in my view:

 <%= link_to add_interest_seller_listings_path(m), method: :post do %>
    Add Interest & Fire Email!
 <%end%>

And in the interest controller, I have this action that that the post in the routes file calls:

 def add_interest
     current_user.mark_buyer_interest(seller_listing)

Which references this in the user.rb:

 def mark_buyer_interest(listing)
     buyer_interests.create(seller_listing: listing.id, accepted_by_seller: 0)
 end

This works well, records are created, but the Mailer is skipped. Any suggestions? I've never done it this way before so any advice would be great. Thank you!

I think you should call UserMailer.interest_to_seller(@interest).deliver_now again in mark_buyer_interest .

def mark_buyer_interest(listing)
  new_interest = buyer_interests.create(seller_listing: listing.id, accepted_by_seller: 0)
  UserMailer.interest_to_seller(new_interest).deliver_now
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM