简体   繁体   English

如何从link_to帖子中调用控制器中的邮件程序动作

[英]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: 我正在尝试呼叫我的interests控制器中的create动作中的邮件:

  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. 当我转到CRUD / interest / new表单并在其中创建它时,此方法效果很好,但实际上是在尝试从其他位置的post方法调用此方法。 It's as if the create actions is being skipped all together, but that's impossible because the record is being created, right? 好像一起跳过了create动作,但这是不可能的,因为正在创建记录,对吗?

My routes.rb: 我的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: 在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. 这很好用,创建了记录,但是跳过了Mailer。 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 . 我认为您应该在mark_buyer_interest再次调用UserMailer.interest_to_seller(@interest).deliver_now 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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