1

所以我遵循了这个有缺陷的教程: http: //matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/

...关于制作联系表格。它工作得很好。唯一的问题是它只发送主题。我认为问题可能出在通知邮件中:

通知邮件程序.rb

class NotificationsMailer < ActionMailer::Base

  default :from => "noreply@youdomain.dev"
  default :to => "you@youremail.dev"

  def new_message(message)
    @message = message
    mail(:subject => "[YourWebsite.tld] #{message.subject}")
  end

end

当然,我希望它发送用户提交的所有信息......(姓名、电子邮件地址、主题和正文。

另外我想知道如何仅使用主题设置为默认值的主体来做一个简单的版本。(我想要一个小的评论框,它会向我发送一封带有评论的电子邮件。)我是否必须为此制作一个全新的控制器和模型,或者这两者都可以处理?

更新

通知邮件程序视图 / new.html.erb

Name: <%= @message.name %>

Email: <%= @message.email %>

Subject: <%= @message.subject %>

Body: <%= @message.body %>

接触控制器

class ContactController < ApplicationController
def new
    @message = Message.new
  end

  def create
    @message = Message.new(params[:message])

    if @message.valid?
      NotificationsMailer.new_message(@message).deliver
      flash[:success] = "Message was successfully sent."
      redirect_to(root_path)
    else
      flash[:error] =  "Please fill all fields."
      render :new
    end
end

end

消息.rb

class Message

  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :name, :email, :subject, :body

  validates :name, :email, :subject, :body, :presence => true
  validates :email, :format => { :with => %r{.+@.+\..+} }, :allow_blank => true

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end

end

基本上它可以工作......但它只发送主题。我还让它发送了一封完整的邮件,除了主题之外的所有内容......但我不记得我是怎么做到的。

我应该把这台电脑砸成一百万块然后横冲直撞吗?

叹...

再次更新

这是使用上述设置的电子邮件所说的:

主题:[liquid.radio] 不管主题是什么。身体:完全空白

这是他们在两周前我做了什么之后说的。

Subject: Message from liquid.radio
Body: 
A contact enquiry was made by Richard Pryor at 2013-06-17 23:36.

Reply-To: richard@pryor.com 
Subject: Scared for no reason Body: Oh
no... Oh God no! What is that?!

我所做的只是弄乱了通知控制器。虽然我不记得……为了我的一生……我做了什么。但是,正如您所看到的……它会发送应有的完整消息……但主题完全不同。

这里真的有点需要帮助。

4

2 回答 2

1

首先,该项目将生产设置放在config/application.rb. 将 GMail ActionMailer 设置移动到config/environments/production.rb. 将letter_openergem 添加到 Gemfile 的开发组。

# Gemfile
group :development do
  gem 'letter_opener'
end

将 letter_opener 设置添加到您的开发环境中。

# config/environments/development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :letter_opener
config.action_mailer.perform_deliveries = true 
config.action_mailer.raise_delivery_errors = true

添加active_attr宝石

# Gemfile
gem 'active_attr'

确保你运行bundle install

用健壮的 ActiveAttr 实现替换您的消息模型。

# app/models/message.rb
class Message
  include ActiveAttr::Model

  attribute :name
  attribute :email
  attribute :subject
  attribute :body

  validates_presence_of :name, :email, :subject, :body
  validates :email, :format => { :with => %r{.+@.+\..+} }, :allow_blank => true
end

改善路线。

# config/routes.rb
get 'contact' => 'contact#new', :as => 'contact'
post 'contact' => 'contact#create', :as => 'contact'

确保您的电子邮件模板正确无误

# app/views/notifications_mailer/new_message.text.erb
Name: <%= @message.name %>
Email: <%= @message.email %>
Subject: <%= @message.subject %>
Body: <%= @message.body %>

更新:2013 年 12 月 12 日 我在 GitHub 上创建了一个 Rails 3.2 项目,以帮助任何人寻找在 Rails 中创建联系表单的现代解决方案。圣诞节快乐! https://github.com/scarver2/contact_form_app

于 2013-07-26T13:58:36.053 回答
0

最终这就是我所做的......我意识到这不是一个真正的解决方案。但我仍然没有不同的。

通知邮件程序.rb

class NotificationsMailer < ActionMailer::Base
  default :from => "example@gmail.com"
  default :to => "example@gmail.com"

  def new_message(message)
    @message = message
    mail(:subject => "[liquid.radio] #{message.subject}", :body => "
    From: #{message.name}
    Reply to: #{message.email}
    Subject: #{message.subject}
    Message:  #{message.body}")
  end

end

根本不是邮件程序应该如何工作......但至少它发送了一条完整的消息。

如果您正处于时间紧缩状态...就像我一样...这会让您到达那里。一旦我不再以任何其他方式收到空白电子邮件,我将接受一个真正的答案(可能是 Scarver2 的)。

于 2013-07-26T19:15:37.527 回答