我想使用ruby mail_form 和Heroku Sendgrid来允许用户向我发送电子邮件。我在 apps / models / contact.rb
中设置了以下联系人课程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
注释掉我的Contact类中定义的电子邮件属性会在后续属性消息:和昵称
中产生类似的错误以下是我的通讯录控制器 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>
非常感谢任何帮助或建议!
答案 0 :(得分:0)
您在from
选项中省略了双引号。
def headers
{
:subject => "Contact Form",
:to => "example@email.com",
:from => %("#{name}" <#{email}>)
}
end