我是新来的Rails,我有一个测试。我只是安装和设置了午餐应用程序所需的一切,我确实有一个现有的应用程序,我需要修改和编写一个简单的发送电子邮件代码点击,
下面是代码:
<%= 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>
我非常感谢任何帮助:)
1条答案
按热度按时间z9ju0rcb1#
考虑查看ActionMailer(Ruby on Rails框架的电子邮件接口)的文档:https://guides.rubyonrails.org/action_mailer_basics.html
1.使用rails cli命令生成邮件程序
1.编辑生成的
app/mailers/user_mailer.rb
1.编辑你的
app/views/layouts/mailer.html.erb
或文本版本如果你喜欢1.将其与您的控制器/触发电子邮件发送的正确路由连接。请参阅本节https://guides.rubyonrails.org/action_mailer_basics.html#calling-the-mailer和本节中的路由https://guides.rubyonrails.org/routing.html。在您的示例中,您的按钮指向
root_path
,这需要通过此电子邮件操作更改为指向正确控制器的路径(一个链接到你在例子中写的视图代码的控制器)如果视图/控制器术语没有意义-考虑阅读MVC模式:https://www.freecodecamp.org/news/the-model-view-controller-pattern-mvc-architecture-and-frameworks-explained/