最近Rails项目的最后两个功能就是进行邮件通知和上传附件管理,这里使用了Rails框架下的ActionMailer和paperclip两个来完成的
一、ActionMailer使用
首先要忽略邮件的错误,进入config/environments,打开development.rb文件,将config.action_mailer.raise_delivery_errors设定为false
config.action_mailer.raise_delivery_errors = false
然后打开test.rb,找到config.action_mailer.delivery_method,将其改为:smtp,默认是:test,在默认情况下,邮件是不会发出去的
config.action_mailer.delivery_method = :smtp
接下来设定发送邮件的服务器和账号密码等信息,修改config/environments/development.rb或config/environments/production.rb都可以,如果使用Gmail发送,可以使用下面的代码:
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host: "http://localhost:3000" }
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:authentication => "plain",
:user_name => "example@gmail.com",
:password => "123456",
:enable_starttls_auto => true
}
当然由于被墙的原因,Gmail不太好使,所以如果想用163服务,则可以这样写:
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host: "http://localhost:3000" }
config.action_mailer.smtp_settings = {
:address => "smtp.163.com",
:port => "25",
:authentication => "login",
:user_name => "xxxxx@163.com",
:password => "xxxxx",
:enable_starttls_auto => true
}
注意使用163邮箱时,千万不要加入:domain参数,这样会发送失败!!
然后可以生成一封Email模板,比如下面:
rails generate mailer UserMailer notify_comment
此时会产生app/mailers/user_mailer.rb文件,打开该文件可以看到
class UserMailer < ActionMailer::Base
default :from => "foobar@example.org"
def notify_comment(user, comment)
@comment = comment
mail(:to => user .email, :subject => "New Comment")
end
end
其中default :from是默认的发信邮箱,这里需要将其改为前面填好的邮箱:user_name => "xxxxx@163.com",然后:to就是收信人的地址,而:subject就是邮件的主题了。
此外,邮件的模板在app/views/user_mailer/notify_comment.text.erb和notify_comment.html.erb,一个是纯文本模式,一个是html模式。
如果要发送邮件,可以在自己的代码种加上如下代码:
UserMailer.notify_comment(user, comment).deliver_now!
这样就可以将邮件发送成功了。
二、paperclip使用
paperclip是一个图片附件的管理组件,非常方便,在使用这个之前必须确保已经安装了ImageMagick这个工具,如果没有可以安装
Mac下就用brew
brew install imagemagick
Linux用户应该可以使用apt-get吧。。。
sudo apt-get install imagemagick
注意安装完成后要确定imagemagick这次程序默认在/usr/local/bin/路径下,否则要修改config/environments/development.rb
Paperclip.options[:command_path] = "/usr/local/bin/"
接下来安装paperclip,打开Gemfile,添加
gem "paperclip", "~> 4.2"
然后bundle install
一般上传附件都是提交在一个表单的时候,因此一般表单都对应这一个model,这样需要对这个model添加一个属性来记录这个附件,比如model为User
首先添加migration
rails g migration add_avatar_columns_to_users
然后在生成的migration文件中这样写:
class AddAvatarColumnsToUsers < ActiveRecord::Migration
def up
add_attachment :users, :avatar
end
def down
remove_attachment :users, :avatar
end
end
然后执行数据库迁移命令
rake db:migrate
接下来在HTML页面的提交表单中添加 添加附件的按钮
<%= form_for @user, :url => users_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %>
<% end %>
点击提交后,此时会由controller处理,因此需要在强壮参数中添加avatar属性,
def create
@user = User.create( user_params )
end
private
# Use strong_parameters for attribute whitelisting
# Be sure to update your create() and update() controller methods.
def user_params
params.require(:user).permit(:avatar)
end
最后在model文件夹里的user.rb中添加数据类型验证:
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
这样在最终的展示页面中,可以使用下面的代码来展示附件:
<%= image_tag @user.avatar.url %>
<%= image_tag @user.avatar.url(:medium) %>
<%= image_tag @user.avatar.url(:thumb) %>