发送电子邮件无法使用 Sendgrid & mail_form 用于 heroku 应用程序。我在 Contacts#create 中收到 NoMethodError
send email not working Using Sendgrid & mail_form for heroku app. I get NoMethodError in Contacts#create
您好,我刚刚按照 https://youtu.be/QIoORYeBdhs?list=PL23ZvcdS3XPK9Y4DRU-BiJtiY5L_QhUUq
中的教程设置电子邮件联系操作
我有以下内容:
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] = "Cannot send an email."
render :new
end
end
end
class Contact < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message
def headers
{
:subject => "My Contact Form",
:to => "testing@gmail.com",
:from => %("#{name}" <#{email}>)
}
end
end
我有意见:
views/contacts/new.html.erb
<h1> Say Hello </h1>
<div>
<%=form_for @contact do |f|%>
<h3>Your are now: <%=current_user.email%> </h3>
<%= f.label :name %><br>
<%= f.text_area :name, required: true %>
<%= f.label :email %><br>
<%= f.email_field :email, required: true %>
<%= f.label :message %><br>
<%= f.text_area :message, as: :text %>
<%=f.submit 'Send message', class: 'button' %>
<%end%>
</div>
views/contacts/create.html.erb
<h1> email sent </h1>
<div>
<h3> email Sent! </h3>
</div>
我有 config/routes.rb
resources :textbook_giveaways
resources :textbooks
resources :contacts, only: [:new, :create]
devise_for :users
而当我运行'rake routes'
contacts POST /contacts(.:format) contacts#create
new_contact GET /contacts/new(.:format) contacts#new
当我点击按钮 'Send message' 时,出现错误:
NoMethodError in Contacts#create
undefined method `length' for nil:NilClass
Extracted source (around line #4):
<% flash.each do |type, msg| %>
<script type="text/javascript">
var duration = parseInt('<%= msg.length %>');
duration = (duration >= 100) ? 6000 : 4000;
noty({
theme: 'bootstrapTheme',
你能帮忙解决一下吗?
更新:
我解决了这个问题,请看下面的答案。
我发现哪里不对了。我修复了 flash[:success] = "Email sent."
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(params[:contact])
#@contact.request = request
if @contact.deliver
flash[:success] = "Email sent."
else
flash[:alert] = "Cannot send an email."
render :new
end
end
end
您好,我刚刚按照 https://youtu.be/QIoORYeBdhs?list=PL23ZvcdS3XPK9Y4DRU-BiJtiY5L_QhUUq
中的教程设置电子邮件联系操作我有以下内容:
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] = "Cannot send an email."
render :new
end
end
end
class Contact < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message
def headers
{
:subject => "My Contact Form",
:to => "testing@gmail.com",
:from => %("#{name}" <#{email}>)
}
end
end
我有意见:
views/contacts/new.html.erb
<h1> Say Hello </h1>
<div>
<%=form_for @contact do |f|%>
<h3>Your are now: <%=current_user.email%> </h3>
<%= f.label :name %><br>
<%= f.text_area :name, required: true %>
<%= f.label :email %><br>
<%= f.email_field :email, required: true %>
<%= f.label :message %><br>
<%= f.text_area :message, as: :text %>
<%=f.submit 'Send message', class: 'button' %>
<%end%>
</div>
views/contacts/create.html.erb
<h1> email sent </h1>
<div>
<h3> email Sent! </h3>
</div>
我有 config/routes.rb
resources :textbook_giveaways
resources :textbooks
resources :contacts, only: [:new, :create]
devise_for :users
而当我运行'rake routes'
contacts POST /contacts(.:format) contacts#create
new_contact GET /contacts/new(.:format) contacts#new
当我点击按钮 'Send message' 时,出现错误:
NoMethodError in Contacts#create
undefined method `length' for nil:NilClass
Extracted source (around line #4):
<% flash.each do |type, msg| %>
<script type="text/javascript">
var duration = parseInt('<%= msg.length %>');
duration = (duration >= 100) ? 6000 : 4000;
noty({
theme: 'bootstrapTheme',
你能帮忙解决一下吗?
更新: 我解决了这个问题,请看下面的答案。
我发现哪里不对了。我修复了 flash[:success] = "Email sent."
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(params[:contact])
#@contact.request = request
if @contact.deliver
flash[:success] = "Email sent."
else
flash[:alert] = "Cannot send an email."
render :new
end
end
end