Godzilla74
Godzilla74

Reputation: 2512

Email form data in rails without model

I have a form that I've created to capture simple contact information from a user:

views/whitepapers/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 %>

Now, Once the user clicks the "Download" button, the file downloads, so I have that part taken care of. Now I'd like to email the form data without saving anything to the DB.

I've created the mailer: mailers/whitepaper_download_mailer.rb

class WhitepaperDownloadMailer < ApplicationMailer
  def email_lead(contact)
    @contact = contact
    mail to: "[email protected]", subject: "A Whitepaper Download!"
  end
end

And I've started working on implementing in the controller, but all the examples I've run across have to do with data including the model. This is what I have so far, but it's not working in my controller:

controllers/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

end

models/whitepaper.rb

class Whitepaper < ActiveRecord::Base
  mount_uploader :whitepaper, WhitepaperUploader

  validates :title, presence: true
  validates :abstract, presence: true
  validates :whitepaper, presence: true
end

Obviously, I know this isn't going to work since I'm passing @contact to the mailer, but pulling form params into a structure (i.e. @contact.name). Should I be passing each of the parameter variables into the mailer:

WhitepaperDownloadMailer.email_lead(@contact.name, @contact.email, @contact.phone).deliver_now

Or is there some other way that I haven't found yet to make this mailer work?

Upvotes: 1

Views: 794

Answers (1)

Godzilla74
Godzilla74

Reputation: 2512

I figured this out with help from @kevinthompson and Openstruct. So, directly from the form, in my controller 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

I also ended up changing the form_tag action in the view to coincide:

<%= form_tag({action: "contact"}, id: "whitepaper-form-#{w.id}") do %>

Upvotes: 1

Related Questions