Error implementing Rails mail_form gem “NameError in ContactsController#new”

问题描述 投票:0回答:0

目标:使用 Ruby(版本 7)和 mail_form gem 创建一个联系表单,以将查询发送到我的 gmail 帐户。我没有附加数据库,只是简单地填写一个表格,然后通过电子邮件将表格数据发给我。

问题:我已经按照 Railcoder 文档进行设置,但我无法让 gem 工作。我已经重新启动服务器几次,运行“bundle install”,检查了 gem 文件。没有人帮助解决问题。

错误:需要文件 /Users/abcd/Code/contact_form/contact-form/app/models/contact.rb 来定义常量联系人,但没有

这是我的设置:

Model: contact.rb
class Contacts < MailForm::Base
  attribute :name,      validate: true
  attribute :email,     validate: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  attribute :message, validate:true
  attribute :nickname,  captcha: true

  def headers
    {
      to: "[email protected]", # change this to be the email you want sent to
      subject: "Railscoder Contact Form",
      from: "[email protected]",  # change this to be the email it is coming from
      reply_to: %("#{name}" <#{email}>)
    }
  end
end
Controller: contacts_controller
class ContactsController < ApplicationController
  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(params[:contact])
    @contact.request = request
    if @contact.deliver
      redirect_to action: :sent
    else
      flas.now[:error] = "Could not send message"
      render :new, status: :unprocessable_entity
    end
  end
end
View: new.html.erb
<div class="px-8 py-6 rounded-md shadow bg-white">
    <h1 class="text-4xl text-center">Contact Railscoder</h1>
    <hr>
    <p>I'd love to hear from you. Send me a message below.</p>

    <%= form_for @contact do |form| %>
    <div class="field mb-6">
        <%= form.label :name %>
        <%= form.text_field :name %>
    </div>

    <div class="field mb-6">
        <%= form.label :email %>
        <%= form.email_field :email %>
    </div>

    <div class="field">
        <%= form.label :message %>
        <%= form.text_area :message %>
    </div>

    <div class="hidden">
        <%= form.label :nickname %>
        <%= form.text_field :nickname %>
    </div>

    <div class="actions flex justify-end">
        <%= form.submit "Send Message" %>
    </div>
    <% end %>
</div>
gem: gem 'mail_form', '~> 1.9'

我仍然在这里学习 Ruby 绳索。所以我不完全确定我在这里遗漏了什么。任何帮助表示赞赏。

我重启了几次服务器,运行“bundle install”,检查了 gem 文件。没有人帮助解决问题。

我已经通过其他 SO 解决方案,但我找不到类似的问题。

ruby-on-rails ruby rubygems mail-form
© www.soinside.com 2019 - 2024. All rights reserved.