Contact:Class 的未定义局部变量或方法“email”
undefined local variable or method `email' for Contact:Class
我想使用 ruby mail_form 和 Heroku Sendgrid 来允许用户给我发送电子邮件。我在 apps/models/contact.rb
中设置了以下联系人 class
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
注释掉我的联系人 class 中定义的电子邮件属性会在后续属性 message: 和 nickname: 中产生类似的错误
下面是我的联系人控制器,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>
如有任何帮助或建议,我们将不胜感激!
您在 from
选项中省略了双引号。
def headers
{
:subject => "Contact Form",
:to => "example@email.com",
:from => %("#{name}" <#{email}>)
}
end
我想使用 ruby mail_form 和 Heroku Sendgrid 来允许用户给我发送电子邮件。我在 apps/models/contact.rb
中设置了以下联系人 classclass 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
注释掉我的联系人 class 中定义的电子邮件属性会在后续属性 message: 和 nickname: 中产生类似的错误
下面是我的联系人控制器,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>
如有任何帮助或建议,我们将不胜感激!
您在 from
选项中省略了双引号。
def headers
{
:subject => "Contact Form",
:to => "example@email.com",
:from => %("#{name}" <#{email}>)
}
end