【问题标题】:Rails contact form not workingRails联系表格不起作用
【发布时间】:2011-05-31 19:43:34
【问题描述】:

我正在尝试创建一个提交表单的联系人。但我没有收到任何电子邮件。

在我的 config/application.rb 我添加了。

config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
     ActionMailer::Base.smtp_settings = {
   :address  => "mail.vinderhimlen.dk",
   :port  => 587,
   :user_name  => "asd@vinderhimlen.dk",
   :password  => "x",
   :authentication  => :login
 }

我的表格:

<%= simple_form_for [@support], :url => { :action => "create" }, :html => { :method => :post } do |f| %>
    <%= f.input :sender_name, :label => 'Navn' %>
    <%= f.input :email, :label => 'E-mail' %>
    <%= f.input :support_type, :collection => ['Feedback', 'Idé', "Rapporter fejl", 'Business', 'Andet'], :prompt => "Valg type", :label => 'Erinde' %>
    <%= f.label :Besked %>
    <%= f.text_area :content, :label => 'Besked', :style => 'width:500px;', %>
    <%= f.submit "submit", :value => 'Send besked' %>
<% end %>

我的支持控制器:

class SupportsController < ApplicationController
  def new
    # id is required to deal with form
    @support = Support.new(:id => 1)
  end

  def create
    @support = Support.new(params[:support])
    if @support.save
      redirect_to('/', :notice => "Support was successfully sent.")
    else
      flash[:alert] = "You must fill all fields."
      render 'new'
    end
  end
end

我的支持模型:

class Support
  include ActiveModel::Validations

  validates_presence_of :email, :sender_name, :support_type, :content 
  # to deal with form, you must have an id attribute
  attr_accessor :id, :email, :sender_name, :support_type, :content

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

  def read_attribute_for_validation(key)
    @attributes[key]
  end

  def to_key
  end

  def save
    if self.valid?
      Notifier.support_notification(self).deliver
      return true
    end
    return false
  end
end

我的配置/环境/开发:

Konkurranceportalen::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # In the development environment your application's code is reloaded on
  # every request.  This slows down response time but is perfect for development
  # since you don't have to restart the webserver when you make code changes.
  config.cache_classes = false

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  config.perform_delivery = true

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_view.debug_rjs             = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = true

  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log

  # Only use best-standards-support built into browsers
  config.action_dispatch.best_standards_support = :builtin
end

提交表单时我的rails登录:

Started POST "/supports" for 127.0.0.1 at 2011-05-31 11:15:35 +0200
  Processing by SupportsController#create as HTML
  Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"bn05TaU4o6TwLVwYH0PgnDyYouo
P1HptzW3HHY2QV/s=", "support"=>{"sender_name"=>"asdasd", "email"=>"ssad@sazdasd.
dk", "support_type"=>"Id├®", "content"=>"asdasd"}, "commit"=>"Send besked"}
  ←[1m←[36mSQL (0.0ms)←[0m  ←[1mSELECT SUM(`tags`.`konkurrancers_count`) AS sum_
id FROM `tags`←[0m
  ←[1m←[35mSQL (8.0ms)←[0m  describe `kategoris_konkurrancers`
  ←[1m←[36mKonkurrancer Load (1.0ms)←[0m  ←[1mSELECT `konkurrancers`.* FROM `kon
kurrancers`←[0m
  ←[1m←[35mCACHE (0.0ms)←[0m  SELECT `konkurrancers`.* FROM `konkurrancers`
  ←[1m←[36mTag Load (1.0ms)←[0m  ←[1mSELECT `tags`.* FROM `tags`←[0m
Rendered notifier/support_notification.html.erb (1.0ms)

Sent mail to asd@vinderhimlen.dk (1752ms)
Date: Tue, 31 May 2011 11:15:39 +0200

From: ssad@sazdasd.dk

To: asd@vinderhimlen.dk

Message-ID: <4asdqweb124ce_16cc85248bc677b3@Home-Pc.mail>

Subject: =?UTF-8?Q?New_Id=C3=A9?=

Mime-Version: 1.0

Content-Type: text/html;

 charset=UTF-8

Content-Transfer-Encoding: quoted-printable



=EF=BB=BFhello world!

asdasd=

Redirected to http://localhost:3000/
Completed 302 Found in 4503ms

【问题讨论】:

  • 在您的 config/environments/development.yml 中,您应该有一行 perform_delivery,将其设置为 true
  • 我试过了还是不行。我已经用我的 development.rb 更新了我的帖子
  • 还有这个? config.action_mailer.perform_deliveries = true
  • 知道我收到 OpenSSL 错误:主机名与服务器证书不匹配
  • 那是另一个问题,邮件被触发发送。你应该问另一个问题

标签: ruby-on-rails ruby ruby-on-rails-3


【解决方案1】:

要在开发模式下触发邮件,请将其添加到你的 development.yml 文件中:

config.action_mailer.perform_deliveries = true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-31
    • 2016-10-28
    • 2014-10-04
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多