mail_form attributes in email ruby on rails

144 views Asked by At

I am coding a site for a client who wants a form that sends information directly to her email.

I was able to set up the form perfectly and am recieving the emails in the designated inbox, however I was wondering if it was possible to change the format of the attributes in the mail recieved.

contact.rb

class Contact < MailForm::Base
  attribute :Guest_One_First_Name
  attribute :Guest_One_Surname
  attribute :Guest_Two_First_Name
  attribute :Guest_Two_Surname
  attribute :Children
  attribute :Restrictions
  attribute :Attending
  def headers
    {
      #this is the subject for the email generated, it can be anything you want
      subject: "RSVP",
      to: '[email protected]',
      from: %("#{:Guest_One_First_Name}", "#{:Guest_One_Surname}"),
      reply_to: %("#{:Guest_One_First_Name}")
      #the from will display the name entered by the user followed by the email
    }
  end
end

controller

class ContactsController < ApplicationController
  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(params[:contact])
    @contact.request = request
    if @contact.deliver
      redirect_to root_path, notice: "RSVP'd Sucessfully"
    else
      flash.now[:error] = 'RSVP not taken into account'
      render :new
    end
  end
end

html

<div class="contact-form">
  <%= simple_form_for @contact do |f| %>
    <% if flash[:error].present? %>
      <p> Please correctly complete the fields below</p>
    <% end %>
    <div class="form-row">
      <div class="col">
        <%= f.input :Guest_One_First_Name, label:"First Name", error: 'Name is mandatory, please specify one', class:"form-control"%> <br>
        <%= f.input :Guest_One_Surname, label:"Surname", error: 'Name is mandatory, please specify one', class:"form-control"%> <br>
      </div>
      <div class="col">
        <%= f.input :Guest_Two_First_Name, label:"First Name", error: 'Name is mandatory, please specify one', class:"form-control"%> <br>
        <%= f.input :Guest_Two_Surname, label:"Surname", error: 'Name is mandatory, please specify one', class:"form-control"%> <br>
      </div>
    </div>
      <div class="form-row">
        <div class="col">
          <%= f.input :Children, label:'Number of Children', class:"form-control"%> <br>
        </div>
      </div>
    <div class="form-row">
      <div class="col">
        <%= f.collection_check_boxes :Attending, [['Brunch'] ,['Ceremony']], :first, :last %>
      </div>
    </div>
    <div class="form-text">
      <%= f.label 'Dietary Restrictions' %>
      <%= f.text_area(:Restrictions, rows: 3, class:"form-control")%> <br>
    </div>
    <div class="send-button">
    <%= button_tag(type: 'submit', class: "suggest-btn-send") do %>
      <p>RSVP</p>
    <% end %>
    </div>
  <% end %>
</div>

and then this is what I recieve in my email:

RSVP Guest one first name: Marie

Guest one surname: L...

Guest two first name: Marie

Guest two surname: S....

Children: 4

Restrictions: none

Attending: ["", "Brunch", "Ceremony"]

I would like to have the attending not be in form of array, and would want to have the information read:

Guest one: Marie L.... Guest two: Marie S... Attending: Brunch, Ceremony.

Please help !

1

There are 1 answers

0
hgunnery On

Simply run .join(', ') on your array and it will create a string for you.

Given your example you may want to do a little clean up on the array first to make sure that there are no blank values otherwise you may get ", Brunch, Ceremony"

Use something akin to .reject(&:empty?) on your array to remove the empties.