2017-08-31 162 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

类的'一些代码,太感谢你了,我知道,我忘了什么东西我无法找出它。 –