使用rails Mail_form gem时出错:“未初始化常量Mailform”

发布于 2024-12-25 13:53:06 字数 2697 浏览 4 评论 0原文

因此,我已经在 stackoverflow 和其他地方检查了类似问题的所有现有答案,但无法让 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 code>:

<%= 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 %>

我对 Rails 和 Web 编程总体来说还很陌生,所以很可能我在这里遗漏了一些在任何在线教程中都没有提到的非常基本的东西。我已经浏览了 mail_form 文档以及我能找到的每个教程和答案,但仍然收到错误。

哦是的!如果我在 Rails 控制台中尝试 ContactForm.new,也会收到错误。任何帮助将不胜感激!

哦,是的,Rails 版本 3.1.1

So, I've checked all the existing answers to similar questions here on stackoverflow and elsewhere, but cannot get the mail_form gem to work as advertised.

Here's the setup: I'm trying to create a simple lead capture form for my company's website. I want to email the data collected by the form to my email account without a database backend and I thought that's what mail_form would make easy.

Here is my model, 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

And here's my controller, 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

And here is my view, 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 %>

I'm pretty new to rails and web programming in general, so it might well be that I am missing something REALLY basic here that isn't mentioned in any online tutorials. I've been all over the mail_form documentation and every tutorial and answer I can find and I'm still getting the error.

Oh yes! I also get the error if I try ContactForm.new in the rails console. Any help would be greatly appreciated!

Oh yes, Rails version 3.1.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

公布 2025-01-01 13:53:06

如果您是第一次从控制台尝试此操作,请务必在安装 gem 后重新启动 Rails 控制台。

If you're trying this for the first time from console, be sure to restart your rails console after installing your gem.

腹黑女流氓 2025-01-01 13:53:06

你的类定义中有一个拼写错误

class ContactForm < Mailform::Base  

,还应该

class ContactForm < MailForm::Base

确保你安装了 mail_form 添加

gem "mail_form", ">= 1.3.0"

到你的 Gemfile 中并运行捆绑安装

you have a typo in your class definition

class ContactForm < Mailform::Base  

should be

class ContactForm < MailForm::Base

also make sure you installed mail_form added

gem "mail_form", ">= 1.3.0"

to your Gemfile and run bundle install

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文