Rails 3 中的联系我们功能

发布于 2024-09-18 15:33:19 字数 179 浏览 9 评论 0 原文

我想在 Rails 3 中制作一个包含以下字段的联系我们表单:

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

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

I want to make a contact us form in Rails 3 with the following fields:

  • Name
  • Email
  • Message title
  • Message body

The posted messages are intended to go to my email address so I don't neccessarily must store the messages in the database. Do I have to use ActionMailer, any gem or plugin for it?

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

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

发布评论

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

评论(4

菊凝晚露 2024-09-25 15:33:19

这个教程是一个很好的例子 - 并且这是 Rails 3

更新:

这篇文章是一个比我之前发布的更好的例子,工作完美

第二次更新:

我还建议合并此railscastactive_attr gem,Ryan Bates 将引导您完成为联系页面设置 tabless 模型的过程。

第三次更新:

我编写了自己的关于它的测试驱动博客文章

This tutorial is an excellent example - and it's Rails 3

Update:

This article is a better example than the one I posted earlier, works flawlessly

Second Update:

I would also recommend merging-in some of the techniques outlined in this railscast on the active_attr gem, where Ryan Bates walks you through the process of setting up a tabless model for a contact page.

Third Update:

I wrote my own test-driven blog post about it

猫瑾少女 2024-09-25 15:33:19

我更新了实现,使其尽可能接近 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 => "[email protected]",
      :from => %("#{name}" <#{email}>)
    }
  end
end

这将允许您测试通过控制台发送电子邮件

联系页面

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

# 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'

I updated the implementation to be as close as possible to the REST specification.

Basic setup

You can use the mail_form gem. After installing simply create a model named Message similar as it is described in the documentation.

# 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 => "[email protected]",
      :from => %("#{name}" <#{email}>)
    }
  end
end

This will already allow you to test sending emails via the console.

Contact page

In order to create a separate contact page do the following.

# 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

Setup the routing ..

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

Prepare a form partial ..

// 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'

And render the form in a view ..

// app/views/messages/index.html.haml
#contactform.row
  = render 'form'
浴红衣 2024-09-25 15:33:19

我无法使这个示例的代码工作,并且我认为自从创建模型以来它使事情变得有点复杂。

无论如何,我制作了一份工作联系表并在博客上介绍了它。文本是葡萄牙语,但代码本身(大部分)是英语http://www.rodrigoalvesvieira.com/formulario-contato-rails/

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

I couldn't make the code of this example work and I think it makes things a bit complex since your creating a model.

Anywat, I made a working contact form and blogged about it.. the text is in portuguese but the code itself is (mostly) in english http://www.rodrigoalvesvieira.com/formulario-contato-rails/

Note: I used sendmail, not SMTP.

反差帅 2024-09-25 15:33:19

您可以通过以下链接使用“联系我们”gem:https://github.com/JDutil/contact_us
文档很清晰,可以简单使用。

特点:

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

You can use Contact Us gem via this link: https://github.com/JDutil/contact_us
The documentation is clear and you can use it simply.

Features:

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