mail_form在rails4上使用ruby无效

时间:2014-02-28 05:32:08

标签: ruby-on-rails ruby ruby-on-rails-4

我在轨道上使用ruby中的gem mail_form和gem simple_form 4.但是当我发送邮件时,错误会导致执行时间到期。我不知道我的表单有什么问题。

   def create
     @contact = Contact.new(params[:contact])
     if @contact.valid?
       @contact.deliver  # error line is here
     else 
       render :new
     end
   end

1 个答案:

答案 0 :(得分:0)

以下是我们正在使用的一些代码:


<强>控制器

#Form submitted
def enquire
    @message = ContactForm.new(enquiry_params)
    flash.clear

    if @message.valid?
        @message.deliver
        flash[:notice] = "Message Sent Thank You!"
    else
        flash[:error] = "Sorry, You Got Errors!"
    end

    respond_to do |format|
        format.html { redirect_to contact_index_path }
        format.js
    end
end

private

def enquire_params
     params.require(:contact_form).permit(:name, :email, :message)
end

<强>邮件程序

我们已对其进行设置,以便表单使用名为“ContactForm”的邮件程序:

#app/mailers/contact_form.rb
class ContactForm < MailForm::Base
  attribute :name,      validate: true
  attribute :email,     validate: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  attribute :message

  #Simple SPAM Protection (needs to remain blank)
  attributes :nickname,  captcha: true

  # Declare the e-mail headers. It accepts anything the mail method
  # in ActionMailer accepts.
  def headers
    {
      subject: "Website Enquiry",
      to: AppConfig["company"]["enquiry"],
      from: %("#{name}" <#{email}>)
    }
  end
end

查看

然后使用:

#app/views/enquiry_mailer/new_message.text
Email: <%= @message.email %>

Subject: <%= @message.subject %>

Message: <%= @message.body %>

你确定你拥有所有这些吗?