我正在尝试在Ruby on Rails应用上安装联系页面。它似乎很直接,但在安装邮件宝石后,用:创建我的控制器:
$ rails generate controller contact_form new create
我导航到我的联系人网址(/ contact_form / new),然后显示
“无法自动加载常量ContactFormController,预期 /home/ubuntu/workspace/app/controllers/contact_form_controller.rb to 定义它“
路线和控制器如下:
的routes.rb
get 'contact_form/new'
get 'contact_form/create'
resources :contact_forms
contact_form_controller.rb
class ContactFormsController < ApplicationController
def new
@contact_form = ContactForm.new
end
def create
begin
@contact_form = ContactForm.new(params[:contact_form])
@contact_form.request = request
if @contact_form.deliver
flash.now[:notice] = 'Thank you for your message!'
else
render :new
end
rescue ScriptError
flash[:error] = 'Sorry, this message appears to be spam and was not delivered.'
end
end
end
contact_form.rb
class ContactForm < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message
attribute :nickname, :captcha => true
# Declare the e-mail headers. It accepts anything the mail method
# in ActionMailer accepts.
def headers
{
:subject => "My Contact Form",
:to => "your_email@example.org",
:from => %("#{name}" <#{email}>)
}
end
end
答案 0 :(得分:1)
请注意,您的班级名为ContactFormsController
,而Rails正在寻找ContactFormController
。你需要特别注意Rails中的复数。
那么为什么Rails会寻找ContactFormController
?由于您的路线未正确定义:
get 'contact_form/new'
get 'contact_form/create'
get 'contact_forms/new'
是表单创建新资源的正确途径。您不使用GET创建资源,因此请删除get 'contact_form/create'
。
resources :contact_forms
实际上是all that you need。
所以要解决这个错误你应该:
contact_form_controller.rb
- &gt; contact_forms_controller.rb
。/contact_forms/new
。