嗨,我刚刚按照教程设置了来自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
当我单击“发送消息”按钮时,出现错误:
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',
你能帮忙吗?
更新: 我解决了这个问题,请看下面的答案。