任何人都可以帮我解决以下错误。我正在尝试发送电子邮件,但无法发送并抛出一些错误。
错误:
Errno :: ECONNREFUSED在UsersController #create中 无法建立连接,因为目标计算机主动拒绝它。 - 连接(2) app / controllers / users_controller.rb:8:在'create'
中我的代码片段如下所示。
视图/用户/ index.html.erb
<h1>Send email to your friend</h1>
<%= form_for @user,:url => {:action => 'create'} do |f| %>
<%= f.text_field:name,placeholder:"Enter your name" %><br>
<%= f.email_field:email,placeholder:"Enter your email" %><br>
<%= f.submit "Send" %>
<% end %>
视图/用户/ new.html.erb
<h1>Successfully registered</h1>
视图/用户/ success.html.erb
<h1>Email sent successfully</h1>
控制器/ users_controller.rb
class UsersController < ApplicationController
def index
@user=User.new
end
def create
@user=User.new(users_params)
if @user.save
UserMailer.registration_confirmation(@user).deliver
redirect_to :action => 'success'
else
render :'index'
end
end
def new
end
def success
end
private
def users_params
params.require(:user).permit(:name, :email)
end
end
视图/ user_mailer文件/ registration_confirmation.text.erb
<%= @user.name%>
<h1>Thank you for registering</h1>
Click <%= link_to "here",users_new_path %>
·邮寄包装/ user_mailer.rb
class UserMailer < ApplicationMailer
default :from => "w5call.w5rtc@gmail.com"
def registration_confirmation(user)
@user=user
mail(:to => user.email, :subject => "Registered")
end
end
配置/初始化/为setup_mail.rb
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => "w5call.w5rtc@gmail.com",
:password => "w5rtc123@",
:authentication => "plain",
:enable_starttls_auto => true
}
endActionMailer::Base.default_url_options[:host] = "localhost:3000"
development.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 and disable caching.
config.consider_all_requests_local = 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
# 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
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
config.action_mailer.delivery_method = :smtp
end
的routes.rb
Rails.application.routes.draw do
root 'users#index'
post "users/create" => "users#create"
get "users/success" => "users#success"
get "users/new" => "users#new"
end
实际上我指的是tutorial。我使用的是rails-4和ruby 1.9.3。请帮我解决这个错误。