在网上找了关于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
现在就可以啦,试试吧
分享到:
相关推荐
在Ruby on Rails框架中,Paperclip是一个非常流行的用于处理文件上传的库。它提供了一种简单而优雅的方式来管理和处理模型中的附件,如图片、文档等。Paperclip与ActiveRecord紧密集成,使得在Rails应用中添加文件...
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 ...
Paperclip 是 Rails 框架的一个插件,用于扩展 ActiveRecord 以支持简单的文件附件的功能。 标签:Paperclip
RUBY的经典之作,对其在RAILS下开发写得很详细
通过以上分析,我们可以看出,《CoffeeScript Programming with jQuery, Rails, and Node.js》这本书涵盖了CoffeeScript在Web开发领域的广泛应用,不仅包括了与主流前端库jQuery的结合,还深入探讨了其在Ruby on ...
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 ...
Ajax(Asynchronous JavaScript and XML)技术则允许在不刷新整个页面的情况下更新部分网页内容,提供更流畅的用户体验。在地图应用中,Ajax可以用于无感知地加载地图数据,如搜索结果或实时位置更新。书中会讲述...
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 ...
标题《Rails3 device and cancan》与描述《ROR ruby on rails device plugin教程》指出本文是关于如何在Rails 3.2应用程序中整合Devise认证插件和Cancan授权插件的教程。Devise是一个流行的Ruby on Rails的认证解决...
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.
Rails还提供了许多插件和gem(Ruby的库),如Devise用于用户认证,CanCanCan进行权限控制,以及Paperclip或Carrierwave用于文件上传等。这些工具大大扩展了Rails的功能,书中可能会提及一些常用的gem及其用法。 ...
回形针 回形针旨在用作ActiveRecord的简单文件附件库。 其目的是使设置尽可能简单,并尽可能将文件与其他属性一样对待。 这意味着它们不会保存到磁盘上的... Paperclip现在需要Ruby版本> = 2.0.0和Rails版本3.2,> =
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 ...
《Ruby on Rails: Up and Running》是一本针对初学者和有经验开发者的技术书籍,它深入浅出地介绍了如何使用Ruby on Rails框架构建Web应用程序。Ruby on Rails(简称Rails)是基于Ruby编程语言的一个开源Web应用框架...
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 ...
### Ruby on Rails与J2EE:两者之间是否有共存的空间? #### 什么是Ruby on Rails? Ruby on Rails(简称Rails)是一种基于Ruby语言构建的相对新兴的Web应用框架。该框架被设计为现有企业级框架的一种替代方案,其...