2017-08-31 73 views
0

我有一個設置在rails中的郵件程序,除了實際應該傳遞的消息外,一切正常。我正嘗試從正在發送的電子郵件內的數據庫中呈現數據。我不斷收到一個沒有方法的錯誤。以下是我有:Rails 5郵件程序方法錯誤

contact_recieved.html.erb

<td valign="top"> 
    <h2 style="color: #0f060f; font-size: 22px; text-align: center;"><%[email protected]%></h2> 
    <p style="color: #0f060f; font-size: 14px; line-height: 22px; text-align: center;">Name: <%[email protected]%></p> 

    <p style="color: #0f060f; font-size: 14px; line-height: 22px; text-align: center;">Email: <%[email protected]%></p> 
    <p style="color: #0f060f; font-size: 14px; line-height: 22px; text-align: center;">Message: <%[email protected]%></p> 
    </td> 

這裏是我的contact_controller.rb:

class ContactsController < ApplicationController 
    def new 
    @contact = Contact.new 

    end 
    def show 
    @contact = Contact.find(params[:id]) 
    end 

    def create 
    # fail 
    @contact = Contact.create(contact_params) 
    if @contact.save 
     ContactMailer.contact_received(@contact).deliver 
     redirect_back(fallback_location: root_path) 
    else 
     flash[:error] = @contact.errors.full_messages 
     redirect_back(fallback_location: root_path) 
    end 


    end 
    private 
    def contact_params 
    params.require(:contact).permit(:name, :subject, :email, :message) 
    end 
end 

最後一點,這裏是我contact.rb型號:

class Contact < ApplicationRecord 
    email_regex = /\A([\w+\-].?)[email protected][a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i 

    validates :name, :presence => true, 
       :length   => { :maximum => 50 } 
    validates :subject, :presence => true, 
       :length   => { :maximum => 50 } 
    validates :email, :presence => true, 
       :format   => {:with => email_regex } 
    validates :message, :presence => true, 
       :length   => { :maximum => 5000 } 

end 

當我提交表單時,我不斷收到一個沒有方法的錯誤,請參閱下圖:

error message

中的數據被存儲在當我提交數據庫證明如下:

second image

這裏是contact_mailer.rb

class ContactMailer < ApplicationMailer 
    default from: "[email protected]***********" 



    def contact_received(contact) 

    mail(to: "*******.com", subject: "This is just a test from Jay") 
    end 
end 

UPDATE

我改變@contacts到@contact和我仍然得到同樣的錯誤信息:

third error

+0

請一些ContactMailer' – rony36

回答

0

更新您的郵件類變量(@contact = contact)分配。

class ContactMailer < ApplicationMailer 
    default from: "[email protected]***********" 



    def contact_received(contact) 
    @contact = contact 

    mail(to: "*******.com", subject: "This is just a test from Jay") 
    end 
end 
+0

類的'一些代碼,太感謝你了,我知道,我忘了什麼東西我無法找出它。 –