我是 Web 开发的新手,目前正在学习 Rails。我制作了一个简单的联系表格,我可以通过控制台发送电子邮件,如 MailForm 页面所示
c = StaticPage.new(:name => 'José', :email => 'jose@email.com', :message => >'Cool!') c.deliver
当我通过我的表格做同样的事情时,虽然我不断收到“无法发送消息”的提示。这是我的代码
路线.rb
Rails.application.routes.draw do
get 'password_resets/new'
get 'password_resets/edit'
root 'static_pages#home'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/signup', to: "users#new"
post '/signup', to: "users#create"
get '/help', to: "static_pages#help"
get '/about', to: "static_pages#about"
get '/contact', to: "static_pages#contact"
resources :users #connects to the show action in users_controller
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :edit, :create, :update]
resources :static_pages, only: [:index, :new, :create]
end
static_pages_controller.rb
class StaticPagesController < ApplicationController
def new
@contact = StaticPage.new
end
def index
@contact = StaticPage.new(params[:home])
end
def create
@contact = StaticPage.new(params[:home])
# @contact.deliver if @contact.valid?
@contact.request = request
respond_to do |format|
if @contact.deliver
# re-initialize Home object for cleared form
@contact = StaticPage.new
format.html { render 'index'}
format.js { flash.now[:success] = @message = "Thanks for contacting us!" }
else
format.html { render 'index' }
format.js { flash.now[:error] = @message = "Cannot send message." }
end
end
end
def help
end
def about
end
# def contact
# end
end
静态页面.rb
class StaticPage < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message
attribute :nickname, :captcha => true
# Declare the e-mail headers. It accepts anything the mail method
# in ActionMailer accepts.
def headers
{
:subject => "Contact You",
:to => "mymail@gmail.com",
:from => %("#{name}" <#{email}>)
}
end
end
_contact_form.html.erb
<%= form_for :contact, url: static_pages_path, remote: true do |f| %>
<div class="col-md-6 col-md-offset-3">
<%= f.label :name %></br>
<%= f.text_field :name, required: true, class: "contact-form-text-area" %></br>
<%= f.label :email %></br>
<%= f.text_field :email, required: true, class: "contact-form-text-area" %></br>
<%= f.label :message %></br>
<%= f.text_area :message, rows: 8, cols: 40, required: true, class: "contact-form-text-area",
placeholder: "Send to Page."%></br>
<div class= "hidden">
<%= f.label :nickname %>
<%= f.text_field :nickname, :hint => 'Leave this field blank!' %>
</div>
<%= f.submit 'Send Message', class: 'btn btn-primary' %>
</div>
<% end %>
<div class="col-md-6" id="flash-message">
<%= render 'flash' %>
</div>
发展.rb
Rails.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 web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
Show full error reports.
config.consider_all_requests_local = true
# Enable/disable caching. By default caching is disabled.
if Rails.root.join('tmp/caching-dev.txt').exist?
config.action_controller.perform_caching = true
config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => 'public, max-age=172800'
}
else
config.action_controller.perform_caching = false
config.cache_store = :null_store
end
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = 'rails-tutorial-myid.c9users.io'
config.action_mailer.default_url_options = { host: host, protocol: 'https' }
config.action_mailer.perform_caching = false
#Settings to deliver form email to gmail
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"],
authentication: 'plain',
enable_starttls_auto: true,
openssl_verify_mode: 'none'}
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Suppress logger output for asset requests.
config.assets.quiet = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
end
日志
Started POST "/static_pages" for 10.240.1.50 at 2018-01-18 10:35:20 +0000
Cannot render console from 10.240.1.50! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by StaticPagesController#create as JS
Parameters: {"utf8"=>"✓", "contact"=>{"name"=>"Jane", "email"=>"jane@gmail.com", "message"=>"wwwwwwwwwwwwwwwwwwwwwwww", "nickname"=>""}, "commit"=>"Send Message"}
Rendering static_pages/create.js.erb
Rendered static_pages/_flash.html.erb (0.5ms)
Rendered static_pages/_contact_form.html.erb (5.1ms)
Rendered static_pages/_flash.html.erb (0.1ms)
Rendered static_pages/create.js.erb (8.0ms)
Completed 200 OK in 23ms (Views: 11.0ms | ActiveRecord: 0.0ms)
我的 gmail id 和密码保存在另一个文件中。我也尝试在配置中使用纯值。此时,来自控制台的那个已经交付了。
我已经浏览了这个平台上的所有相关帖子以及谷歌上的很多帖子,但仍然无法弄清楚为什么它不会交付。我的猜测是这与我的控制器有关。非常感谢任何帮助。