简体   繁体   English

Rails:是否可以使用link_to或button_to方法触发邮件程序方法?

[英]Rails: Is it possible to Trigger a mailer method with a link_to or button_to method?

I'm trying to create an admin Tool where the admins can send emails that are already there with a button. 我正在尝试创建一个管理工具,管理员可以在其中使用按钮发送已经存在的电子邮件。

I have a class FamilyMailer < ActionMailer::Base with this method birthdaymailer 我有一个class FamilyMailer < ActionMailer::Base用这种方法birthdaymailer

Is there a way for me to trigger this from the view ? 我有办法从视图中触发此事件吗?

I have seen a lot of answers on StackOverFlow, but it doesn't really work. 我在StackOverFlow上看到了很多答案,但实际上并没有用。

The closest conclusion I have came across is that the Mailer gets triggered this way FamilyMailer.birthdaymailer in the view and create a return to main page. 我遇到的最接近的结论是,邮件程序是通过这种方式在视图中触发FamilyMailer.birthdaymailer并创建返回到主页的。

I was wondering if there would be a better way. 我想知道是否会有更好的方法。

Sure! 当然!

Say you've got a mailer, called UserMailer. 假设您有一个名为UserMailer的邮件程序。

Set up your controller action: 设置控制器动作:

# my_controller.rb

def send_mail
  @parameters = Model.get_parameters
  UserMailer.name_of_action(@parameters).deliver
end

Set up your route: 设置路线:

get send_mail, to: 'my_controller#send_mail', as: :send_mail

Then make your link! 然后建立您的链接!

link_to 'Send mail', send_mail_path, class: "big-button"

Voila.

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

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