转移 http://aikin.me/2014/02/20/rails-paperclip/
Paperclip是 Rails 的一个上传图片插件,它和ImageMagick联合使用,可以很方便的实现图片上传并切割指定大小的功能,使整个图片上传过程非常简单,还会将上传的图片保存在public文件夹下。
1.下载paperclip gem包
(1)在Gemfile中写上
gem 'paperclip'
(2)在终端运行
bundle install
2.使用paperclip
(1)在终端输入:
rails g paperclip image_news image
这样就会在原先的 image_news model中增加image属性,同时在app/db/migrate 文件夹下会
新建xxxxxxx_add_attachment_image_to_image_news.rb 文件内容:
class AddAttachmentImageToImageNews < ActiveRecord::Migration def self.up change_table :image_news do |t| t.attachment :image end end def self.down drop_attached_file :image_news, :image end end
(2)再对数据库迁移,终端输入:
rake db:migrate
如果你原先的image_news model 中没有其他属性,那么在schema.rb文件中:
ActiveRecord::Schema.define(version:xxxxxxx) do create_table "image_news", force: true do |t| t.datetime "created_at" t.datetime "updated_at" t.string "image_file_name" t.string "image_content_type" t.integer "image_file_size" t.datetime "image_updated_at" end end
(3)接着在image_news.rb文件中写上:
class ImageNews < ActiveRecord::Base has_attached_file :image, //:styles => { :small => "150x150>"}, :url => '/images/:id/:style/:basename.:extension', :path => ':rails_root/public/images/:id/:style/:basename.:extension' validates_attachment_presence :image validates_attachment_size :image, :less_than => 5.megabytes validates_attachment_content_type :image, :content_type => %w(image/jpeg image/png) end
上面代码,要使用 :styles 就必须要确认是否有装 imageMagick,如果没装就会报错。
还有 :path :rails_root/public 后面的必须和 :url相同,就像这里的
'images/:style/:basename.:extension'
:rails_root The path to the Rails application.(当前Rails程序的路径,等同于RAILS_PATH的值。) :rails_env The current environment (e.g. development, production)(当前运行环境,如:development,production) :class The class name of the model that the attachment is part of, underscored and pluralised for your convenience.(文件拥有者的类名的复数形式,多个单词间以下划线连接。) :basename The name of the originally uploaded file without its extension.(上传的文件原始的名字,不带扩展名。) :extension The file extension of the originally uploaded file.(上传的文件的扩展名) :id The ID of the model that the attachment is part of.(文件拥有者的id) :id_partition The same as :id but formatted as a string using ID partitioning.(和:id一样,但结果是用ID partitioning格式化过的。) :attachment The name of the attachment attribute (defined in the call to has_attached_file) downcased and pluralised for your enjoyment.(文件作为拥有者的属性的属性名,也就是跟在has_attached_file后面的那个名字,小写、复数形式。) :style The current style of the attachment file being processed (e.g. in the ‘discarding an uploaded image‘ example above the :style would be one of ‘original’ or ‘small’) (文件被以某种样式处理的样式名,该选项用于图片处理。)
(4) 在controller(controllers/image_upload_controller.rb)里写上:
class ImageUploadController < ApplicationController def image_upload_view @image_news = ImageNews.new end def upload @image_news = ImageNews.new @image_news.image = params[:image_news][:image] @image_news.save redirect_to(:action => 'image_show_view', :id => @image_news.id) end def image_show_view @image_news = ImageNews.find(params[:id]) end end
(5) 在view(views/image_upload_view.html.erb)写上:
<div style="background: #F5F5F5; width: 659px; padding: 50px; margin: 100px auto auto auto"> <%= form_for @image_news, :url =>{:action => 'upload'}, :html => {:multipart => true} do |f| %> <p>请选择图片:</p> <%= f.file_field :image%> <%= f.submit '上传' %> <% end %> </div>
(6)在view/image_show_view.html.erb写上:
<div style="margin: 100px 80px 100px 500px"> <%= image_tag @image_news.image.url%> </div>
(7) 安装ImageMagick (Mac OS X 10.8.5) 安装很成功、方便,不过得最先装xcode。
curl -O ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz tar -zxf ImageMagick.tar.gz cd ImageMagick-*/ ./configure --prefix=/opt/local make sudo make install
也可以用Homebrew安装。
参考:http://yuan.iteye.com/blog/604174
http://huacnlee.com/blog/rails-plugin-paperclip-for-image-upload/
http://railscasts.com/episodes/134-paperclip
http://elf8848.iteye.com/blog/455675
相关推荐
NULL 博文链接:https://mylir.iteye.com/blog/800734
在 Rails 4.0.0 上运行带有 AWS S3 演示的 Paperclip 教程这是一个教程和演示,用于获取在 Rails 4.0.0 上运行的带有 AWS S3 上传的 Paperclip。 在本教程中,我们将创建一个简单的站点,可以在其中创建文章并可以将...
每一个当您将应用程序部署到,您正在更改RAILS_ROOT,因此您所有上传的文件都将丢失,您的存储桶将一团糟! 默认的Heroku工作路径是默认路径。 如何使用 使用以下命令将宝石添加到您的Gemfile中 gem “paperclip-...
插件使用jrails(jQuery)和Paperclip插件来支持图像和媒体上传特征提供富文本编辑器可自定义的TinyMCE插件易于整合支持图像上传和插入支持媒体上传和YouTube嵌入待办事项:文档上传插件安装将此行添加到您的应用...
Rails.root / app / models / user.rb-包含Mongoid :: Paperclip模块并调用提供的类方法class User include Mongoid :: Document include Mongoid :: Paperclip has_mongoid_attached_file :avatarend就是这样那...
技术: 我使用 Ruby on Rails,使用 ruby gem Devise 进行身份验证,使用 Paperclip 上传照片,使用 Amazon S3 存储照片,使用 ruby gem Geocoder 查找您所在位置附近的甲虫。 项目部署在Heroku上: : 流程...
[构建状态]( ) 上传相册、裁剪照片并查看查看过您相册的人的分析。 截图: 主页: 图像裁剪器: 分析: 在尝试一下 对于最新版本的 Photozz,您还可以将 repo 克隆下来并在您自己的机器上试用: git clone git@...
自述 这篇文章和教程提供了如何使用 Ruby/Rails 构建基本的创建/读取/更新/删除 (CRUD) 应用程序的背景知识。 这篇文章的重点是如何设置文件附件系统并将其投入生产,使用来自 Thoughtbot、Heroku 和 Amazon 为 ...
设计 - 验证用户 Bootstrap - 通用样式 Paperclip - 上传和显示图像 Mailboxer - 帮助消息传递功能(包括回复) Bootstrap Material Design - 扩展设计 Masonry Rails - 通过过滤组织用户的显示(稍后显示) ...
这个 Clone 是作为 Ruby on Rails 教程制作的,并使用 ... 特别强调使用设计 gem、Pins、使用 Paperclip gem 上传图像、使用 act_as_votable gem 投票、Masonry jQuery 和 HAML 的用户。 有关更多信息/教程,请访问 。
使用Paperclip gem上传图像,编辑和删除图像23.砌体和靴子的风格24.使用宝石对销钉进行投票25.新建,编辑,帐户登录/注销页面的最终抛光 17. Pinterest副本入门 ===== $ rails new yourname_pinterest ===== 更新您...
它受到 Paperclip 的影响(和复制),并大量使用了令人难以置信的 Fog 库。 要求 宝石经过以下测试: Ruby on Rails 4.1.6 Ruby 2.1.6 Ruby 2.0.0 Ruby 1.9.3 Ruby 安装 gem install attached 可选 brew ...
轻松提问 受 Quora 启发的问题应用程序。 特征 用户帐户。 用户可以提出问题。 用户可以回答问题。 用户可以对问题、答案或其他用户发表评论。... 使用 Paperclip 和 Amazon AWS 上传和存储文件 使用 pg_search
ActiveRecord :: Base attr_accessible :description , :title attr_accessible :image has_attached_file :imageend 为了处理附件,我正在使用 Paperclip。 我已经从下载并安装了 Plupload 在我连接了一些 ...
实现项目的图像上传使用了Paperclip gem。 您可以通过文件或网址上传。 信息使用formData类存储,该类在AJAX发布请求中发送。 还搜索用户。 尚无法预测,但将来会增加。 播种的用户是: rweir11-演示 rweir12 ...
文件验证器 File Validators gem将文件大小和内容类型验证添加到ActiveModel。 任何使用ActiveModel的模块,例如... 经过测试,可以与Carrierwave,Paperclip,Dragonfly,Refile等文件上传解决方案一起使用。 验
# 使用 Rails 4、jQuery 和 Paperclip 上传多个文件这是我的博客文章的 git 存储库:http: 。 这个存储库保存了我文章中描述的所有内容的工作副本。 如果您有任何问题或需要帮助,请打开一个。 该项目使用 MIT 许可...
关于 Club-Biz 是为软件建模和设计课程设计和构建的社交应用程序。 仅供参考,我们被迫将其命名为“Club-Biz”。...ImageMagick 必须安装在服务器上才能通过 PaperClip gem 上传图像。 在带有自制软件的 UNIX 系统
在演唱会 inConcert是一个受Vimeo启发的Web应用程序,具有音乐上的曲折,是使用Ruby on Rails和React / Redux构建的。 该数据库是我最喜欢的视频的种子,该视频由纽约索法勒声音公司的现场视听团队制作,在那里我...
其中大部分是由自动处理所有上传的 Paperclip (amazing Gem) 处理的。 如果来自 S3 的带宽量变为并发布(其服务的最大费用),您可能需要查看如何缓存图像。 回形针 这是一个很棒的插件,可以处理来自客户端的所有...