我创建了一个表单,用于捕获用户的简单联系信息:
视图/白皮书/ index.html.erb
<%= form_tag({action: "download"}, id: "whitepaper-form-#{w.id}") do %>
<%= label_tag 'name' %>
<%= text_field_tag "contact[name]", nil, class: "form-control" %>
<br/>
<%= label_tag 'email' %>
<%= email_field_tag "contact[email]", nil, class: "form-control" %>
<br/>
<%= label_tag 'phone' %>
<%= text_field_tag "contact[phone]", nil, class: "form-control" %>
<%= hidden_field_tag 'id', w.id %>
<%= hidden_field_tag 'whitepaper-name', w.title %>
<%= submit_tag "Download Now", class: "btn btn-success", id: "whitepaper-# {w.id}-submit" %>
<% end %>
现在,用户点击&#34;下载&#34;按钮,文件下载,所以我有那部分照顾。现在,我想通过电子邮件发送表单数据,而不用将任何内容保存到数据库中。
我创建了邮件: mailers / whitepaper_download_mailer.rb
class WhitepaperDownloadMailer < ApplicationMailer
def email_lead(contact)
@contact = contact
mail to: "admin@example.co", subject: "A Whitepaper Download!"
end
end
我已经开始在控制器中实现,但我遇到的所有示例都与包括模型在内的数据有关。这是我到目前为止所做的,但它在我的控制器中无效:
控制器/ whitepapers.rb
def download
@whitepaper = Whitepaper.find(params[:id])
@contact.name = params[:contact_name]
@contact.email = params[:contact_email]
@contact.phone = params[:contact_phone]
@contact.whitepaper_name = params[:whitepaper_name]
file_path = File.join(Rails.root, "public", @whitepaper.whitepaper_url)
send_file file_path
WhitepaperDownloadMailer.email_lead(@contact).deliver_now
端
模型/ whitepaper.rb
class Whitepaper < ActiveRecord::Base
mount_uploader :whitepaper, WhitepaperUploader
validates :title, presence: true
validates :abstract, presence: true
validates :whitepaper, presence: true
end
显然,我知道这不会起作用,因为我将@contact传递给邮件程序,但是将形式的参数拉入结构(即@ contact.name)。我应该将每个参数变量传递给邮件程序:
WhitepaperDownloadMailer.email_lead(@contact.name, @contact.email, @contact.phone).deliver_now
或者还有其他方法我还没有发现这个邮件可以使用吗?
答案 0 :(得分:1)
我在@kevinthompson和Openstruct的帮助下想出了这个。因此,直接从表单中,在我的控制器 controllers / whitepapers.rb :
中def contact
@whitepaper = Whitepaper.find(params[:contact][:whitepaper_id])
file_path = File.join(Rails.root, "public", @whitepaper.whitepaper_url)
send_file file_path
if request.post?
@contact = OpenStruct.new(params[:contact])
WhitepaperDownloadMailer.email_lead(@contact).deliver_now
end
end
我最后还改变了视图中的form_tag操作:
<%= form_tag({action: "contact"}, id: "whitepaper-form-#{w.id}") do %>