如何使用Ruby on Rails Action邮件程序实现多个附件

时间:2015-04-01 11:58:03

标签: ruby-on-rails ruby ruby-on-rails-3

我正在制作一项功能,允许用户在发送电子邮件时附加多个附件。我添加了 multiple:true 和multipart:true但是无法附加多个附件。请帮帮我怎么做。 另外,请注意,我如何一个接一个地显示上传的文件名。 注意:我正在使用paperclip

我的代码:

Form.html.erb

<%= form_tag mass_mail_reseller_path, :multipart => true, class: "new-mail-form form-horizontal" do%>
        <div class="row form-group">

        <div class="row form-group">
          <%= label_tag "attachment", 'Attachment:', class: "col-sm-1 control-label"%>
          <div class="col-sm-10">
            <%= file_field_tag "attachment",  multiple: true %>
          </div>
        </div>
    <% end %>

my_controller.rb#动作

def mail_reseller
    _from = params[:from]
    _to = get_resellers_mail_Ids(params[:to])
    _subject = params[:subject]
    _content = params[:body]
    if params[:attachment]
      _filename = params[:attachment].original_filename
      _file = params[:attachment].read
    end
    Resque.enqueue(ResellerWorker, _to, _from,  _subject, _content, _filename, _file)
    flash[:notice] = "Mail has been sent."
    redirect_to reseller_admin_path(:project_id => 'lead_tracking_reseller')
    # directory = "#{Rails.root}/files"
    # path = File.join(directory, _filename)
    # File.open(path, "wb") { |f| f.write(params[:attachment].read) }
  end

寄件人/ my_mailer.rb

def mass_mail_resellers _to, _from, _subject, _content, _filename, _file
    attachments[_filename] = _file if (_filename && _file)
    @mail_body = _content
    mail from: _from,
         to: _to,
         bcc: 'jam@company.com',
         subject: _subject
  end

model/resller.rb
class Reseller < User
  has_many :settlements
  belongs_to :account_manager, class_name: 'User', foreign_key: 'account_manager_id'
  belongs_to :author, class_name: 'User', foreign_key: 'author_id'
  has_and_belongs_to_many :plans
  has_many :attachments
end

ResellerWorker代码。

reseller_worker.rb

class ResellerWorker
  @queue = :reseller_mail_queue
  include Redmine::I18n

  def self.perform(_to, _from, _subject, _content, _filename, _file)
    _to.each do |_reseller|
      ResellerMailer.mass_mail_resellers(_reseller, _from, _subject, _content, _filename, _file).deliver
    end
  end

end

0 个答案:

没有答案