Rails 4 ActionMailer从表单创建和发送电子邮件

时间:2016-06-30 22:49:14

标签: forms ruby-on-rails-4 routes actionmailer sendgrid

我是动作管理员的新手,似乎无法使ActionMailer正常工作和路由,不确定我是否正确地执行此操作,大多数教程仅显示如何在用户拥有后生成通用电子邮件首先创建一个帐户。

我要做的是允许用户通过表单生成电子邮件,以发送电子邮件,其中包含指向其客户的视图的链接以查看发票。

我当前的错误是由于一些路由问题,但奇怪的是它在我的控制器中寻找电子邮件模板而不是邮件控制器是我的假设... Missing template invoices/send_invoice_email, application/send_invoice_email

以下是我用来构建电子邮件的表单。

<%= bootstrap_form_tag url: '/send_invoice' do |l| %>
     <%= l.hidden_field :invoice, value: @quote.id %>
          <div class="input-group margin-bottom-20">
            <span class="input-group-addon" id="from"><i class="fa fa-envelope"> </i></span>
            <%= l.text_field :from, hide_label: true, value: current_user.email %>
          </div>
          <div class="input-group margin-bottom-20">
            <span class="input-group-addon" id="to"><i class="fa fa-user"> </i></span>
                      <%= l.text_field :to, hide_label: true, value: @quote.client.contacts.first.email %>
          </div>
          <div class="input-group margin-bottom-20">
            <span class="input-group-addon" id="cc"><i class="fa fa-users"> </i></span>
                      <%= l.text_field :cc, hide_label: true, placeholder: "cc:" %>
          </div>
          <div class="input-group margin-bottom-20">
            <span class="input-group-addon" id="bcc"><i class="fa fa-users"> </i></span>
                      <%= l.text_field :bcc, hide_label: true, placeholder: "bcc:" %>
          </div>
          <div class="input-group margin-bottom-20">
            <span class="input-group-addon" id="subject"> <i class="fa fa-bullhorn"></i></span>
                    <%= l.text_field :subject, hide_label: true, placeholder: "Subject" %>
          </div>
          <div class="input-group margin-bottom-20">
            <span class="input-group-addon" id="message"> <i class="fa fa-comment"></i></span>
                    <%= l.text_area :message, hide_label: true, placeholder: "Message", rows:"5" %>
          </div>
          <%= l.submit "Send", class:'btn-u btn-u-blue btn-block' %>
<% end %>

我的发票控制器中的控制器方法(这只是一个节目控制器,报价是在报价模型中创建的)

def send_invoice_email
quote = Quote.find(params[:invoice])
InvoiceMailer.send_invoice_email(quote, params)
end

这是发票邮件中的方法

def send_invoice_email(quote, params)
@quote = quote

mail(
to: current_user.email,
from: params[:from],
content_type: "text/html",
body: params[:body],
content_type: "text/html",
subject: params[:subject],
content_type: "text/html"
)
end

1 个答案:

答案 0 :(得分:0)

我改变了一些东西,现在它似乎正在起作用......不确定为什么,但我会接受它。这是我目前的配置..

表格是一样的。

在我的发票控制器中,我现在有

def send_invoice_email
  quote = Quote.find(params[:invoice])
  InvoiceMailer.invoice_email(quote, params).deliver_now
end

在我的发票邮件中,我现在有

def invoice_email(quote, params)
  @user = quote.user
  @quote = quote
  @message = params[:body]
  mail to: params[:to],
      from: @user.email,
      body: @message,
      subject: params[:subject],
      content_type: "text/html"
end