【问题标题】:Contact us functionality in Rails 3Rails 3 中的联系我们功能
【发布时间】:2011-04-11 00:33:51
【问题描述】:

我想在 Rails 3 中使用以下字段与我们联系:

  • 姓名
  • 电子邮件
  • 消息标题
  • 消息正文

发布的消息会发送到我的电子邮件地址,因此我不必将消息存储在数据库中。我必须使用ActionMailer,任何 gem 或插件吗?

【问题讨论】:

标签: ruby-on-rails forms email contact mail-form


【解决方案1】:

This 教程是一个很好的例子——它是 Rails 3

更新:

This article 是一个比我之前发布的更好的例子,完美运行

第二次更新:

我还建议在active_attr gem 上合并this railscast 中概述的一些技术,Ryan Bates 将引导您完成为联系人页面设置表格模型的过程。

第三次更新:

我为此写了自己的test-driven blog post

【讨论】:

  • 这已经过时并且使用了不必要的支持模型。虽然仍然 +1,因为它提高了我的理解 :)
  • 同意 Abe,我添加了一些新链接等
  • 您是否知道如何对您链接到的文章模型中概述的 attr_accessors 进行 i18n 化?我在 .yml 中定义了以下内容,但在相应的错误消息中忽略了它。 de.activerecord.attributes.message.subject: Betreff
  • 您在翻译中引用了 ActiveRecord。我认为您需要使用 ActiveModel。尝试de.activemodel.attributes.message.subject: Betreff,并对您的语言环境文件进行必要的更改。将extend ActiveModel::Translation 添加到您的模型中也可能会有所帮助,但我不知道这是否绝对必要。看到这个相关的github issue
【解决方案2】:

我无法使此示例的代码正常工作,而且我认为自从您创建模型以来它使事情变得有些复杂。

Anywat,我制作了一个有效的联系表格并在博客上发布了它。文字是葡萄牙语,但代码本身(大部分)是英文http://www.rodrigoalvesvieira.com/formulario-contato-rails/

注意:我使用的是 sendmail,而不是 SMTP。

【讨论】:

    【解决方案3】:

    我更新了实现以尽可能接近 REST 规范。

    基本设置

    您可以使用mail_form gem。安装后只需创建一个名为 Message 的模型,类似于文档中的描述。

    # app/models/message.rb
    class Message < MailForm::Base
      attribute :name,          :validate => true
      attribute :email,         :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
      attribute :message_title, :validate => true
      attribute :message_body,  :validate => true
    
      def headers
        {
          :subject => "A message",
          :to => "contact@domain.com",
          :from => %("#{name}" <#{email}>)
        }
      end
    end
    

    这已经允许您测试sending emails via the console

    联系页面

    要创建单独的联系页面,请执行以下操作。

    # app/controllers/messages_controller.rb
    class MessagesController < ApplicationController
      respond_to :html
    
      def index
      end
    
      def create
        message = Message.new(params[:contact_form])
        if message.deliver
          redirect_to root_path, :notice => 'Email has been sent.'
        else
          redirect_to root_path, :notice => 'Email could not be sent.'
        end
      end
    
    end
    

    设置路由..

    # config/routes.rb
    MyApp::Application.routes.draw do
      # Other resources
      resources :messages, only: [:index, :create]
      match "contact" => "messages#index"
    end
    

    准备一个部分表单..

    // app/views/pages/_form.html.haml
    = simple_form_for :contact_form, url: messages_path, method: :post do |f|
      = f.error_notification
    
      .form-inputs
        = f.input :name
        = f.input :email, label: 'Email address'
        = f.input :message_title, label: 'Title'
        = f.input :message_body, label: 'Your message', as: :text
    
      .form-actions
        = f.submit 'Submit'
    

    并在视图中呈现表单..

    // app/views/messages/index.html.haml
    #contactform.row
      = render 'form'
    

    【讨论】:

    • 我试试这个,但是,你把你的 SMTP 配置放在哪里。而且这个联系表可以在localhost环境下工作吗?
    • @Stanmx SMTP 配置进入config/environment/development.rb../production.rbdocumentation describes a setup for GMail 在本地主机上运行时也可以使用。
    • 谢谢@JJD,我从另一个例子开始,但我每次发送,我收到:错误的参数(Fixnum)! (预期类型的​​ OpenSSL::SSL::SSLContext)
    • @Stanmx 请打开一个描述您的设置的新问题。随意在此处链接。
    【解决方案4】:

    您可以通过此链接使用联系我们 gem:https://github.com/JDutil/contact_us 文档一目了然,你可以简单地使用它。

    特点:

    1. 验证
    2. 简单/添加删除字段
    3. 简单配置

    【讨论】:

      猜你喜欢
      • 2012-01-13
      • 2017-12-03
      • 1970-01-01
      • 2016-07-28
      • 1970-01-01
      • 2012-02-26
      • 1970-01-01
      • 2011-12-01
      • 1970-01-01
      相关资源
      最近更新 更多