因此,我已經檢查了所有現有的類似問題的答案,這裏是在stackoverflow和其他地方,但無法讓mail_form gem工作爲廣告。使用rails的錯誤Mail_form gem:「未初始化的常量郵件表單」
以下是設置:我正在嘗試爲我公司的網站創建簡單的潛在客戶表單。我想通過電子郵件將表單收集的數據發送到我的電子郵件帳戶,而不需要數據庫後端,我認爲這就是mail_form的簡單之處。
這裏是我的模型,ContactForm.rb
:
class ContactForm < Mailform::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w]{2,})\z/i
attribute :file, :attachment => true
attribute :phone
attribute :referral
attribute :message
attribute :nickname, :captcha => true
def persisted?
false
end
def headers
{
:subject => "New Lead",
:to => "[email protected]",
:from => %("#{name}" <#{email}>)
}
end
end
這是我的控制器,contact_forms_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 interest!'
redirect_to root_path
else
render :new
end
rescue ScriptError
flash[:error] = 'Sorry, something was wrong'
end
end
end
這裏是我的看法,contact_forms/new.html.erb
:
<%= form_for @contact_form do |f| %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name, :required => true %>(required)<br /><br><br>
</div>
<div class="field">
<%= f.label :email %>
<%= f.email_field :email, :required => true %>(required)
<br /><br><br>
</div>
<div class="field">
<%= f.label :phone %>
<%= f.phone_field :phone %><br /><br><br>
</div>
<div class="field">
How did you hear about us?:<br /> <%= f.text_field :referral
%><br /><br><br>
</div>
<div class="field">
Comments (What types of wine are you
interested in?):<br/<%= f.text_area :message %><br /><br><br>
</div>
<div class="field">
Submit: <%= f.submit "Create" %>
</div>
<% end %>
我通常對軌道和網絡編程來說很新,所以很可能是我在這裏沒有任何在線教程中提到了一些非常基本的東西。我已經遍佈mail_form文檔,每個教程和答案都可以找到,而且我仍然遇到了錯誤。
哦,是的!如果我在rails控制檯中嘗試ContactForm.new,我也會收到錯誤消息。任何幫助將不勝感激!
哦,是的,Rails的版本3.1.1
您的模型文件應該命名爲'contact_form.rb',這就是rails慣例。你還提到了一個錯誤,你可以將它添加到你的問題嗎? – 2012-01-06 12:19:44