0

我有一个带有现有联系表的 rails 4.2 应用程序。我如何让 rails 将电子邮件发送到包含消息的地址?mail_form gem 看起来最有希望,但是当我尝试从控制台发送电子邮件时,我在尝试发送时收到“错误”消息。

表格如下:

<%= form_for(enquiry) do |f| %>
  <% if enquiry.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(enquiry.errors.count, "error") %> prohibited this enquiry from being saved:</h2>

      <ul>
      <% enquiry.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :subject, "Subject:" %><br>
    <%= f.text_field :subject, :size => "40" %>
  </div>
  <div class="field">
    <%= f.label :e_description, "Description:" %><br>
    <%= f.text_area :e_description, :cols => "80", :rows => "10" %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我刚刚从自述文件中获得了基本的 ContactForm 模型:

class ContactForm < MailForm::Base
  attribute :customer_name,      :validate => true
  attribute :email,     :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i

  attribute :message

  # Declare the e-mail headers. It accepts anything the mail method
  # in ActionMailer accepts.
  def headers
    {
      :subject => "My Contact Form",
      :to => "my.email@gmail.com",
      :from => %("#{customer_name}" <#{email}>)
    }
  end
end

编辑 - 查询控制器:

class EnquiriesController < ApplicationController

def index
    @enquiries = Enquiry.all
end

def show
end

def new
    @enquiry = Enquiry.new
    render 'pages/contactus'
end

def edit
end

def create
    @enquiry = Enquiry.new(enquiry_params)
    if current_user.customer?
        @enquiry.user_id = current_user.id
    end
    if @enquiry.save
        redirect_to '/pages/contactus', notice: 'Enquiry was successfully sent.'
    else
        render :new
    end
end

def update
    if @enquiry.update(enquiry_params)
        redirect_to @enquiry, notice: 'Enquiry was successfully updated.'
    else
        render :edit
    end
end

def destroy
    @enquiry.destroy
    redirect_to enquiries_url, notice: 'Enquiry was successfully destroyed.'
end

private
# Use callbacks to share common setup or constraints between actions.
def set_enquiry
    @enquiry = Enquiry.find(params[:id])
end

# If resource not found redirect to root and flash error.
def resource_not_found
    yield
rescue ActiveRecord::RecordNotFound
    redirect_to root_url, :notice => "Room not found."
end

# Only allow a trusted parameter "white list" through.
def enquiry_params
    params.require(:enquiry).permit(:subject, :e_description, :user_id)
end
end
4

1 回答 1

1
  <div class="field">
    <%= f.label :e_description, "Description:" %><br>
    <%= f.text_area :e_description, :cols => "80", :rows => "10" %>
  </div>

我认为这是您电子邮件的内容。该表单将其称为 e_description,而在您的 ContactForm 类中,我在第 5 行看到对消息的引用。使它们相同。

添加控制器代码将有助于进一步分析。

另一个问题是第 13 行对“电子邮件”的引用(这应该会产生错误,因为它应该包含在引号中。)尝试;

:from => "#{customer_name} <#{email}>"
于 2015-04-06T09:08:33.773 回答