2014-05-07 48 views
0

我正在關注this tutorial以使用Rails設置Contact Us表格。我希望表格上的內容可以郵寄到我的個人帳戶。我按照上述說明操作,當我運行foreman start時,出現以下錯誤。爲Gmail設置使用RAILS聯繫我們頁面時設置SMTP時出錯

/home/adhithya/.rvm/gems/[email protected]_rails_4_0/gems/railties-4.0.4/lib/rails/railtie/configurable.rb:30:in `method_missing': undefined method `「smtp' for #<PersonalWebsite::Application:0x9b6e4c4> (NoMethodError) 
from /home/adhithya/Desktop/RailsDev/rocky-oasis-9687/config/environments/development.rb:37:in `block in <top (required)>' 
from /home/adhithya/.rvm/gems/[email protected]_rails_4_0/gems/railties-4.0.4/lib/rails/railtie/configurable.rb:24:in `class_eval' 

我還沒有安裝設計雖然,因爲我不需要在我的網站的任何身份驗證,因爲我沒有登錄模式。

我想知道如何讓這個工作。另外,關於使用Rails實現簡單的聯繫我們表單的單頁網站的最佳方式的建議將會很有幫助。

我development.rb看起來像這樣

config.action_mailer.smtp_settings = { 
    address: 「smtp.gmail.com」, 
    port: 587, 
    domain: ENV["GMAIL_DOMAIN"], 
    authentication: 「plain」, 
    enable_starttls_auto: true, 
    user_name: ENV["GMAIL_USERNAME"], 
    password: ENV["GMAIL_PASSWORD"] 
    } 
+1

請粘貼development.rb文件(約37行) – wacaw

+0

我剛剛編輯它。我已經用我的發展更新了這篇文章.rb –

+0

嘗試'insted of'? – wacaw

回答

0

基本和簡單的解決方案是:

#config/routes.rb 
match "contact" => "welcome#contact", :as=> "contact", :via=>[:get,:post] 


#controllers/welcome_controller.rb 
def contact 
    if(params[:name] && params[:message] && params[:email]) 
    NotificationsMailer.send_contact(params[:name] ,params[:email],params[:message]).deliver 
    redirect_to root_path, notice: t('notice.welcome.contact') 
    else 
    render 'welcome/contact' 
    end 
end 

#views/welcome/contact.html.haml 
%form{:method => :post} 
    %h2 Contact form 
    %input{:type=>"hidden", :name=>"authenticity_token", :value=>form_authenticity_token.to_s} 
    %div 
    %label.control-label{:for=>"name"} 
     Name 
    %input{:name=>"name", :type=>"text", :required=>true, :maxlength => 50} 
    %div 
    %label{:for=>"email"} 
     Email 
    %input{:name=>"email", :type=>"email", :required=>true} 

    %div 
    %label.control-label{:for=>"message"} 
     Message 
    %textarea{:name=>"message", :id=>"message", :rows=>'5', :required=>true} 

    %div.form-actions 
    %input.button{:type=>"submit", :value=>"Send"} 

#mailers/notifications_mailer.rb 
def send_contact(name,email,message) 
    @name=name 
    @email = email 
    @message = message 
    mail(:subject=>"Message form contact form") 
end 
+0

我會用無表的模型,而不是親自資源路線 –

+0

您必須更改表單(構建資源表單)並對提交進行操作(更改參數名稱) – wacaw

+1

是的,但是我個人認爲它更清晰 –