Ruby-on-rails 联系我们 Rails 3 中的功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3689730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Contact us functionality in Rails 3
提问by rodrigoalvesvieira
I want to make a contact us form in Rails 3 with the following fields:
我想在 Rails 3 中使用以下字段与我们联系表单:
- Name
- 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?
发布的消息旨在发送到我的电子邮件地址,因此我不必将消息存储在数据库中。我必须使用ActionMailer任何 gem 或插件吗?
回答by stephenmurdoch
Thistutorial is an excellent example - and it's Rails 3
本教程是一个很好的例子——它是 Rails 3
Update:
更新:
This articleis 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 railscaston the active_attrgem, where Ryan Bates walks you through the process of setting up a tabless model for a contact page.
我还建议在active_attrgem上合并此 railscast 中概述的一些技术,Ryan Bates 将引导您完成为联系页面设置 tables 模型的过程。
Third Update:
第三次更新:
I wrote my own test-driven blog postabout it
回答by JJD
I updated the implementation to be as close as possible to the REST specification.
我更新了实现以尽可能接近 REST 规范。
Basic setup
基本设置
You can use the mail_form gem. After installing simply create a model named Messagesimilar as it is described in the documentation.
您可以使用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
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'
回答by rodrigoalvesvieira
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/
无论如何,我制作了一个有效的联系表格并在博客上写了它……文字是葡萄牙语,但代码本身(大部分)是英文的http://www.rodrigoalvesvieira.com/formulario-contato-rails/
Note: I used sendmail, not SMTP.
注意:我使用的是 sendmail,而不是 SMTP。
回答by Aboozar Rajabi
You can use Contact Us gem via this link: https://github.com/JDutil/contact_usThe documentation is clear and you can use it simply.
您可以通过此链接使用联系我们 gem:https: //github.com/JDutil/contact_us文档很清楚,您可以简单地使用它。
Features:
特征:
- Validation
- Easy/Add remove fields
- Simple configuration
- 验证
- 简单/添加删除字段
- 配置简单

