Rails - Mail_form如何自动将“from”字段设置为current_user电子邮件

时间:2016-08-12 13:30:47

标签: ruby-on-rails contact-form mail-form

我遵循了本指南https://rubyonrailshelp.wordpress.com/2014/01/08/rails-4-simple-form-and-mail-form-to-make-contact-form/

在联系表单中,用户填写一个字段以显示其姓名和电子邮件以及消息。

class ContactForm < MailForm::Base

  attribute :name,      :validate => true
  attribute :email,     :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  attribute :message
  attribute :nickname,  :captcha  => true

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

我想从user.email属性自动发送,而不是让用户填写字段。这可能吗?

1 个答案:

答案 0 :(得分:1)

Trh this:

在您的app/views/contacts/new.html.erbemail field设为hidden field并使用current_user.email作为value的{​​{1}}应该有效。

注意:我假设您使用的是hidden email field gem