如何使用 Rails 和 js 和 html 按钮结束电子邮件?

how to end email using Rails and js and html button?

提问人:bilel abdelmoula 提问时间:1/30/2023 更新时间:1/30/2023 访问量:162

问:

我是使用 rails 的新手,我正在尝试找出使用 html 按钮、javascript 函数和 rails 发送电子邮件的解决方案。

HTML 和 JS 代码:

<%= form_tag root_path, class: "send-email" do %>
  <%= submit_tag "Click to Send Email", style: "margin: 10px; padding: 10px" %>
<% end %>

<script>
document.querySelector(".send-email").onsubmit = function(e) {
  e.preventDefault()

  fetch(e.target.action, {
    method: "POST",
    headers: {
      "X-CSRF-Token": document.querySelector('meta[name="csrf-token"]').content
    }
  }).then(function(response) {
    if (response.ok) {
      window.open('/mail', '_blank')
    } else {
      console.error(response)
    }
  });
}
</script>

联系Mailer.rb

class ContactMailer < ApplicationMailer
    def contact 
        mail(to: 'abdelmo[email protected]', subject:'testing for fly')
    end
end

联系MailerPreview.rb

# Preview all emails at http://localhost:3000/rails/mailers/contact_mailer
class ContactMailerPreview < ActionMailer::Preview

    def contact 
        ContactMailer.contact()
    end
end

联系人:.html.erb

<h1>Hello this is for testing the email function</h1>

我不知道如何把所有东西连接在一起。

JavaScript HTML Ruby-on-Rails 电子邮件 Actionmailer

评论

0赞 engineersmnky 1/31/2023
您缺少控制器操作。现在您正在向以下地址发送帖子请求(我们不知道这是哪里)。您需要更改为将到达将发送电子邮件的控制器操作的路径。点击此处了解更多信息root_pathroot_path
0赞 bilel abdelmoula 2/1/2023
请问控制器的代码应该如何?
1赞 engineersmnky 2/1/2023
我建议通读文档,呼叫邮件部分解释了所有这些。

答: 暂无答案