https://gist.github.com/784445
rails new example -d=mysql
cd example
Gemfile 添加
gem 'paperclip'
bundle install
新建文件/lib/paperclip_processors/watermark.rb
module Paperclip
class Watermark < Thumbnail
def initialize(file, options = {}, attachment = nil)
super
@watermark_path = options[:watermark_path]
@position = options[:position].nil? ? "SouthEast" : options[:position]
end
def make
src = @file
dst = Tempfile.new([@basename].compact.join("."))
dst.binmode
return super unless @watermark_path
params = "-gravity #{@position} #{transformation_command.join(" ")} #{@watermark_path} :source :dest"
begin
success = Paperclip.run("composite", params, :source => "#{File.expand_path(src.path)}[0]", :dest => File.expand_path(dst.path))
rescue PaperclipCommandLineError
raise PaperclipError, "There was an error processing the watermark for #{@basename}" if @whiny
end
dst
end
end
end
rails g scaffold image title:string
rails g paperclip image image
rake db:create && rake db:migrate
app/models/image.rb
require 'paperclip_processors/watermark'
class Image < ActiveRecord::Base
has_attached_file :image,
:processors => [:watermark],
:styles => {
:medium => {
:geometry => "300x300>",
:watermark_path => "#{Rails.root}/public/images/rails.png"
},
:thumb => "100x100>",
}
end
app/views/images/_form.html.erb
<%= form_for(@image,:html => {:multipart => true}) do |f| %>
<% if @image.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@image.errors.count, "error") %> prohibited this image from being saved:</h2>
<ul>
<% @image.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div>
<%= f.label :image %>
<%= f.file_field :image %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
app/views/show.html.erb
<p id="notice"><%= notice %></p>
<p>medium:</p>
<%= image_tag(@image.image.url(:medium)) %>
<%= link_to 'Edit', edit_image_path(@image) %> |
<%= link_to 'Back', images_path %>
分享到:
相关推荐
NULL 博文链接:https://mylir.iteye.com/blog/800734
Paperclip 是 Rails 框架的一个插件,用于扩展 ActiveRecord 以支持简单的文件附件的功能。 标签:Paperclip
Rails3教程系列 DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY...
Windows7_Cygwin_Git_RVM_Ruby1.9.3_Rails3_MongoD B_Nginx_Unicorn_Rspec_Guard_Spork(2-Ruby on Rails3 安装配置
turbo-sprockets-rails3, 加速你的Rails 3资产 用于 Rails 3.2.x的涡轮链轮 通过只根据源文件的哈希来重新编译已经更改的资产,从而加快 Rails 3 rake assets:precompile的速度只编译一次以生成指纹和非打印的资产...
3 rails # gem install rails -v=2.3.11 4 passenger # gem install passenger passenger-install-apache2-module 安装完成后按照提示将下面的内容添加到etc/httpd/httpd.conf LoadModule pa
Rails 3 in Action 2011年9月新鲜出炉,针对最新的Rails 3.1进行说明
The Rails™ 3 Way is a comprehensive resource that digs into the new features in Rails 3 and perhaps more importantly, the rationale behind them. —Yehuda Katz, Rails Core The Bible for Ruby on ...
The Rails™ 3 Way is the only comprehensive, authoritative guide to delivering production-quality code with Rails 3. Pioneering Rails expert Obie Fernandez and a team of leading experts illuminate ...
基于java的开发源码-Rails3消息队列系统 Sidekiq.zip 基于java的开发源码-Rails3消息队列系统 Sidekiq.zip 基于java的开发源码-Rails3消息队列系统 Sidekiq.zip 基于java的开发源码-Rails3消息队列系统 Sidekiq.zip ...
Rails3消息队列系统 Sidekiq
Rails3的ActiveRecord 查询API.doc
Ruby+on+Rails+3+Tutorial.pdf 应用Rails进行敏捷Web开发第4版.pdf (Agile Web Development with Rails) Rails.Recipes.Rails.3.Edition.pdf
rails3的英文文档
在 Rails 4.0.0 上运行带有 AWS S3 演示的 Paperclip 教程这是一个教程和演示,用于获取在 Rails 4.0.0 上运行的带有 AWS S3 上传的 Paperclip。 在本教程中,我们将创建一个简单的站点,可以在其中创建文章并可以将...
rails常用命令,例如新建rails项目,新建controller、model 等等
ruby on rails开发的初学者很适合的工具书,易上手,很实用,
This pioneering book is the first resource that deep dives into the new Rails 3 APIs and shows you how use them to write better web applications and make your day-to-day work with Rails more ...
否则,它将为Rails 3和Rails 4实现or方法。支持Ruby2.2〜2.7 Rails 3.2、4.2、5.0、5.1、5.2、6.0安装将此行添加到您的应用程序的Gemfile中: gem 'rails_or' 然后执行: $ bundle或将其自己安装为: $ gem install...