首页 > 解决方案 > 邮件 ruby​​ on rails 中的 mail_form 属性

问题描述

我正在为一个需要将信息直接发送到她的电子邮件的表单的客户编写一个网站。

我能够完美地设置表单并在指定的收件箱中接收电子邮件,但是我想知道是否可以更改收到的邮件中属性的格式。

联系人.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: 'mariagenourcolas@gmail.com',
      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

控制器

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>

然后这是我在电子邮件中收到的内容:

回复客人的名字:玛丽

嘉宾一姓:L...

嘉宾二名:玛丽

嘉宾二姓:S....

儿童:4

限制:无

出席:["", "Brunch", "Ceremony"]

我希望参加者不是数组形式,并且希望阅读以下信息:

嘉宾一:Marie L.... 嘉宾二:Marie S... 出席:早午餐,仪式。

请帮忙 !

标签: ruby-on-railsemailmail-form

解决方案


只需在您的数组上运行 .join(', ') ,它就会为您创建一个字符串。

鉴于您的示例,您可能需要先对数组进行一些清理,以确保没有空白值,否则您可能会得到“, Brunch, Ceremony”

在您的阵列上使用类似于 .reject(&:empty?) 的东西来删除空。


推荐阅读