0

我想使用 ruby​​ mail_form和 Heroku Sendgrid 来允许用户给我发送电子邮件。我在apps/models/contact.rb中设置了以下 Contact 类

class Contact < MailForm::Base

attribute name,    :validate => true
attribute email,   :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i  
attribute message, :validate => true
attribute nickname,:captcha  => true

 def headers 
 {
   :subject => "Contact Form",
   :to      => "example@email.com",
   :from    => %("#{name}" <#{email}>)
 } 
 end
end

但是,当我访问设置表单的页面时,我收到以下错误消息:

undefined local variable or method `email' for Contact:Class

注释掉我的联系人类中定义的电子邮件属性会在后续属性消息中产生类似的错误:昵称:

下面是我的联系人控制器,contacts_controller.rb

class ContactsController < ApplicationController

 def new
   @contact = Contact.new
 end

 def create
   @contact = Contact.new(params[:contact])
   @contact.request = request
   if @contact.deliver
     flash.now[:error] = nil
   else
     flash.now[:error] = "Oops! There was an error."
     render :new
   end
 end

end 

和我的表格new.html.erb

<% provide(:title, "Contact") %>
<h1>Contact</h1>
<%= form_for @contact do |f| %>
<%= f.label :name %><br>
<%= f.text_field :name, required: true %>

<br>

<%= f.label :email %><br>
<%= f.text_field :email, required: true %>

<br>

<%= f.label :message %><br>
<%= f.text_area :message, as: :text %>

<div class="hidden">
<%= f.label :nickname %><br>
<%= f.text_field :nickname, hint: "Leave this field blank." %>  
</div>

<%= f.submit "Send Message", class: "btn" %>
<% end %>
<ul class="pager">
<li><a href="<%= blog_path %>">Previous</a></li>
</ul>

任何帮助或建议将不胜感激!

4

1 回答 1

0

from您在选项中省略了双引号。

 def headers 
 {
   :subject => "Contact Form",
   :to      => "example@email.com",
   :from    => %("#{name}" <#{email}>)
 } 
 end
于 2016-08-27T05:50:35.430 回答