【问题标题】:Rails 3.2.13 Contact Form Only Sending SubjectRails 3.2.13 联系表仅发送主题
【发布时间】:2013-07-24 19:33:09
【问题描述】:

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

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

notifications_mailer.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

message.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?!

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

这里真的有点需要帮助。

【问题讨论】:

  • 请附上这些文件:app/views/notifications_mailer/new_message.text.erbapp/controllers/contact_controller.rbapp/models/message.rb
  • 完成。我把三个都加了。任何帮助将不胜感激。
  • 还有人在吗?
  • 这非常……非常令人沮丧。请告诉我那里有人可以帮忙吗?我即将删除整个内容并找到另一种在rails中制作表格的方法......但我发现的一切都来自2008年......
  • 坚持住。我正在从头开始重建这个项目。教程有点过时了。完成后我会将 GitHub 项目发送给您,并希望有解决方案。 :)

标签: ruby-on-rails-3 ruby-on-rails-3.2 contact-form


【解决方案1】:

首先,这个项目将生产设置放在config/application.rb。将 GMail ActionMailer 设置移动到 config/environments/production.rb。将 letter_opener gem 添加到 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_attrgem

# 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

【讨论】:

  • 我完全做到了所有这些......这一切似乎都很好......但它仍然做同样的事情。发送一封空白电子邮件。此外,该保管箱链接不会让我下载。它显示文件,让我点击下载...然后说它不可用。
  • 您确定该通知邮件程序没有问题吗?
  • 啊。没关系。是我的弹出窗口拦截器。是的,我改变了你所说的一切。我已经一一浏览了所有文件。它完全按照应有的方式发送主题……以及空白的正文。
  • 嗨@DeborahSpeece。你能给我一个链接到你的项目吗?问题可能与通知邮件程序或文件名或目录结构有关。
  • @DeborahSpeece 哦,是的......这是一个问题的噩梦。我真的很感谢 Scarver2 的帮助。 :) 但请等到您到达 activemerchant!
【解决方案2】:

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

notifications_mailer.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)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多