ActiveModel联系表单发送电子邮件但邮件正文未显示

时间:2014-04-17 18:47:13

标签: ruby-on-rails

我有联系表单设置,我可以在提交表单后收到电子邮件。我似乎无法弄清楚如何在电子邮件中包含表单条目。

例如,用户必须填写的两个字段是他们的电子邮件和邮件正文。当我收到电子邮件时,我不会在电子邮件中包含任何用户填写的信息。我收到的唯一信息来自/contact-mailer/new_contact.html.erb里面的Thank you for sending us a message!

contact_mailer.rb:

  def new_contact(contact)
      @contact = contact
      mail(to: 'username@gmail.com', subject: 'Contact us Form')
    end

联系人控制器:

  def create
      @contact = Contact.new(params[:message])
      if @contact.valid?
        ContactMailer.new_contact(@contact).deliver
        flash[:notice] = "Message sent! Thank you for contacting us."
        redirect_to root_url
      else
        render :action => 'new'
      end
    end

联系人型号:

  include ActiveModel::Validations
    include ActiveModel::Conversion
    extend ActiveModel::Naming

    attr_accessor :email, :content

    validates_length_of :content, :maximum => 500

    def initialize(attributes={})
      attributes && attributes.each do |name, value|
        send("#{name}=", value) if respond_to? name.to_sym 
      end
    end

    def persisted?
      false
    end

/contacts/new.html.erb:

<% title "Contact Us" %>

<%= form_for @contact do |f| %>
  <p>
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </p>
  <p>
    <%= f.label :content, "Message" %><br />
    <%= f.text_area :content %>
  </p>
  <p><%= f.submit "Send Message" %></p>
<% end %>

日志:

Started POST "/contacts" for 127.0.0.1 at 2014-04-17 14:38:16 -0400
Processing by ContactsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"WN19FGciesn2QUiaMtxv4Lwo48tRTkFBSud7OPGRTts=", "contact"=>{"email"=>"test@test.com", "content"=>"This should be included in the email."}, "commit"=>"Send Message"}
  User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`auth_token` = 'Mqy5_1kyb4hAsrmB9Q0fug' LIMIT 1
  Rendered contact_mailer/new_contact.html.erb (0.4ms)

Sent mail to username@gmail.com (2687.6ms)
Date: Thu, 17 Apr 2014 14:38:16 -0400
From: no-reply@domain.com
To: username@gmail.com
Message-ID: <53501f989f98b_ed7833109c4539dc@Macintosh.local.mail>
Subject: Contact us Form
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit


<p>Thank you for sending us a message!</p>


Redirected to http://localhost:3000/
Completed 302 Found in 2768ms (ActiveRecord: 0.4ms)

1 个答案:

答案 0 :(得分:1)

您需要更改/contact-mailer/new_contact.html.erb以包含您的内容, 请致电Action Mailer docs

例如:

<p> The following contact registered: </p>
<p> Email: <%= @contact.email %> </p>
<p> Content: <%= @contact.content %> </p>