Ruby on Rails如何使用simple_form和mail_form gem发送带附件的电子邮件。我确实收到了电子邮件,但没有任何附件

时间:2018-02-21 19:54:38

标签: ruby-on-rails ruby simple-form simple-form-for

我正在尝试使用simple_form和mail_form gem发送带附件的电子邮件,但它无效。

我用过:

<%= simple_form_for @payment, html: { multipart: true }, defaults: 
{ wrapper_html: { class: 'form-group' }, input_html: { class: 'form-control' } } do |f| %>
  ...
  <%= f.file_field :file %>

<% end %>

我收到了电子邮件,但没有任何附件。

电子邮件正文

姓名:John

电子邮件:test@email.com

消息:测试电子邮件

文件:#ActionDispatch :: Http :: UploadedFile:0x00007fc9094557c0

这是我的模特

class Payment < MailForm::Base
attribute :name
attribute :cell_phone
attribute :user_code
attribute :email,    validate: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message
attribute :file
attribute :nickname, captcha: true

def headers
    {
      subject: "Comprovante - Contas Atecorp",
      to: "email@email.com",
      from: %("Contas Atecorp" <email@email.com>),
      reply_to: %("#{name}" <#{email}>)
    }
  end
end

这是我的控制器

class PaymentsController < ApplicationController
  def create
    @payment = Payment.new(payment_params)
    @payment.request = request
    if @payment.deliver
      flash.now[:notice] = 'Thank you for your message!'
      redirect_to user_faturas_path(current_user)
    else
      flash.now[:error] = 'Cannot send message.'
      redirect_to user_faturas_path(current_user)
    end
  end

  private

  def payment_params
    params.require(:payment).permit(:name, :email, :message, :nickname,
                                        :cell_phone, :user_code, :file)
  end
end

0 个答案:

没有答案