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
Contact クラスで定義された email 属性をコメント アウトすると、後続の属性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>
どんな助けや提案も大歓迎です!