在网上找了关于paperclip的有关插件,发现按他们说的,设置了大小却没有效果,后来发现需要安装一个图像编辑的软件才行:ImageMagick
下面简单的介绍一下:
环境:Windows + Cygwin + Rails
向项目中的Person类添加上传附件功能
一、安装paperclip:
script/plugin install git://github.com/thoughtbot/paperclip.git
迁移任务:
script/generate paperclip person photo
rake db:migrate
在model/Person.rb中加入:
class Person < ActiveRecord::Base
has_attached_file :photo, :styles => {
:thumb => "100x100#",
:small => "150x150>",
:large => "400x400>" },
:url => "/assets/products/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/products/:id/:style/:basename.:extension"
end
在views/people下的new.html.erb和edit.html.erb中修改form并加入:
<% form_for @person, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :photo %><br />
<%= f.file_field :photo %>
</p>
<% end %>
在views/people下的index.html.erb和show.html.erb中加入:
<%= image_tag person.photo.url(:thumb) %>
<%= image_tag person.photo.url(:small) %>
<%= image_tag person.photo.url(:large) %>
不过现在上传了图片还不能正确的显示出来,还需要:
二、安装ImageMagick
详见:
http://www.imagemagick.org/script/install-source.php#unix
大致是先下载ImageMagick.tar.gz到Cygwin目录下,解压编译安装:
tar xvfz ImageMagick.tar.gz
cd ImageMagick-6.5.1
./configure
make && make install
现在就可以啦,试试吧
分享到:
相关推荐
NULL 博文链接:https://mylir.iteye.com/blog/800734
You should have some experience with basic Rails concepts and a cursory understanding of JavaScript, CSS, and SQL, but by no means need to be an expert. You'll learn how to install Postgres on your ...
This concise teaches you how to use the power of Docker and Kubernetes to deploy your Rails applications easily and efficiently. Docker and Kubernetes are increasing in popularity every day, but what ...
Paperclip 是 Rails 框架的一个插件,用于扩展 ActiveRecord 以支持简单的文件附件的功能。 标签:Paperclip
RUBY的经典之作,对其在RAILS下开发写得很详细
CoffeeScript Programming with jQuery, Rails, and Node.js.pdf 下载
Apress,.Beginning.Google.Maps.Applications.with.Rails.and.Ajax.
You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of developers have used this award-winning book to learn Rails. It’s a broad, far-reaching ...
This short early adopter book details both how you’ll upgrade existing web and other applications from Rails 4.2 to 5 and how to create new applications in Rails 5. The headline features, Rails API ...
Ruby on Rails and J2EE
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 ...
Your Ruby on Rails ...This new edition has been updated to Rails 5.2 and RSpec 3.7 and contains full coverage of new Rails features, including system tests and the Webpack-based JavaScript setup.
回形针 回形针旨在用作ActiveRecord的简单文件附件库。 其目的是使设置尽可能简单,并尽可能将文件与其他属性一样对待。 这意味着它们不会保存到磁盘上的... Paperclip现在需要Ruby版本> = 2.0.0和Rails版本3.2,> =
This short early adopter book details both how you'll upgrade existing web and other applications from Rails 4.2 to 5 and how to create new applications in Rails 5. The headline features, Rails API ...
Ruby on Rails is the revolutionary online programming tool that makes creating functional e-commerce web sites faster and easier than ever. With the intuitive, straightforward nature of Ruby and the ...
Rails, Angular, Postgres, and Bootstrap(2nd),电子书实战版
Ruby on Rails strips complexity from the development process, enabling professional developers to focus on what matters most: delivering business value via clean and maintainable code. The Rails™ 3 ...
This second edition of the bestselling Crafting Rails Applications has been updated to Rails 4 and discusses new topics such as streaming, mountable engines, and thread safety. ☆ 出版信息:☆ [作者...
Each recipe has been updated to reflect the latest features of Rails 3 and each lays out a distinctive solution to a problem you may be facing today or could well encounter tomorrow. In addition, you...