Rails 5 Mailer没有方法错误

时间:2022-11-22 18:09:31

I have a mailer set up in rails everything is working fine except the actual message that is supposed to be delivered. I am trying to render data from the database inside of the email that is being sent out. I keep getting a no method error. Here is what I have:

我有一个邮件设置在rails中一切正常,除了应该传递的实际消息。我正在尝试从发送的电子邮件中的数据库中呈现数据。我一直没有方法错误。这是我有的:

contact_recieved.html.erb

contact_recieved.html.erb

 <td valign="top">
    <h2 style="color: #0f060f; font-size: 22px; text-align: center;"><%=@contact.subject%></h2>
    <p style="color: #0f060f; font-size: 14px; line-height: 22px; text-align: center;">Name: <%=@contact.name%></p>

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

Here is my contact_controller.rb:

这是我的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

And lastly here is my contact.rb model:

最后这是我的contact.rb模型:

class Contact < ApplicationRecord
    email_regex = /\A([\w+\-].?)+@[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

When I submit the form I keep getting a no method error see image below:

当我提交表单时,我一直收到无方法错误,请参阅下图:

Rails 5 Mailer没有方法错误

The data is getting stored in the database when I submit it as evidenced below:

当我提交数据时,数据将存储在数据库中,如下所示:

Rails 5 Mailer没有方法错误

Here is the contact_mailer.rb

这是contact_mailer.rb

class ContactMailer < ApplicationMailer
    default from: "info@***********"



  def contact_received(contact)

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

UPDATE

UPDATE

I changed @contacts to @contact and I am still getting the same error message:

我将@contacts更改为@contact,我仍然收到相同的错误消息:

Rails 5 Mailer没有方法错误

1 个解决方案

#1


1  

Update your mailer class to assign the variable (@contact = contact).

更新邮件程序类以分配变量(@contact = contact)。

class ContactMailer < ApplicationMailer
  default from: "info@***********"



  def contact_received(contact)
    @contact = contact

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

#1


1  

Update your mailer class to assign the variable (@contact = contact).

更新邮件程序类以分配变量(@contact = contact)。

class ContactMailer < ApplicationMailer
  default from: "info@***********"



  def contact_received(contact)
    @contact = contact

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