2014-02-28 16 views
1

我在rails上使用gem mail_form和gem simple_form ruby​​ 4.但是當我發送一個郵件時產生一個錯誤,執行時間過期了。我不知道我的表單有什麼問題。mail_form無法在rails4上使用ruby4

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

我試圖清理代碼,使其可讀,但我迷路了,你實際上正在嘗試做 - 'end' /'else'沒有任何意義,而你錯過了'end' 。 – sevenseacat

+0

在'end'之後放置'else'沒有意義 – RSB

回答

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 %> 

確定你有這一切嗎?