Step 1:  Install one of these three image manipulators:

-ImageScience,
-minimagic, or
-RMagick.

n00b's should note:
The ImageScience and minimagick sites referenced above offer almost no instructions on getting them installed. From the blog posts I have seen around, getting those to work is more complicated than one would think. Even non-n00bs have difficulty with these - they require copying dll's directly to windows directories, installing those dll's using command prompt, and somehow correctly referencing them in RoR.

For windows at least, RMagick is the only one I found to provide decent instructions, so I would start there.

RMagick will require you to install one windows application (included in the zip file), and then to install the gem using the local source code

NOTE: The gem referenced by the usual online source, RubyForge, is for Unix-type installs only. If your on windows, you should download and uncompress the RMagick zip file, install the windows software, then copy the rmagick*.gem file to your c:\ drive (or some other easilly accesible folder (the file can be deleted after install)

Inside a rails shell, this is the syntax to install a local gem:

gem install -l C:/rmagick-2.10.0-x86-mswin32.gem


If you insist on using ImageScience with Windows, you may likely find this blog useful:
http://thewebfellas.com/blog/2008/2/18/imagesci...

Step 2: Install attachment fu rails plugin.


The following should do the trick from a Rails Shell:

script/plugin install http://svn.techno-weenie.net/projects/plugins/attachment_fu/


Step 3 - generate a controller named projimages (plural) with three views.


script/generate controller projimages new index show

projimage_controller.rb

def new

@projimage = Projimage.new

end


def create

@projimage = Projimage.new(params[:projimage])

if @projimage.save

flash[:notice] = 'Project Image was successfully created.'

redirect_to projimage_url('/mlab/@projimage')

else

render :action => :new

end

end


def index

@projimages = Projimage.find(:all, :conditions => "thumbnail is null")

end


def show

@projimage = Projimage.find(params[:id])

end



The conditional find in the index method is because of the way the attachment_fu stores items in the projimages table - one row is for non-thumbs, and another row is for thumbs -which reference the row above it using parent_id. So essentially, this conditional find returns the non-thumbnails, and attachment_fu does the rest to reference items correctly as needed.

Sidenote: If n00bs wanted to see the actual table data storage, I recommend using the SQLite Manager addon to firefox (you need to google it) to view your database tables. Very awesome tool in general.

Step 4 - Generating your controller above should have also created a view directory called projimages with three html.erb files.  Replace the code with the following:


index.html.erb

<h1>All Project Images</h1><br/>
Click on the thumbnail to see larger size.<br/>
<% for projimage in @projimages -%>
<%= link_to image_tag(projimage.public_filename(:thumb)), projimage.public_filename %>
<% end -%>

new.html.erb

<%= error_messages_for :projimage %>

<% form_for(:projimage, :url => projimages_path,
:html => { :multipart => true }) do |f| -%>
<p>
<label for="projimage">Upload A Project Image:</label>
<%= f.file_field :uploaded_data %>
</p>
<p>
<%= submit_tag 'Create' %>
</p>
<% end -%>

show.html.erb - contains one line of code:


<%= link_to image_tag(@projimage.public_filename(:thumb)), @projimage.public_filename %>


Step 5 - generate a model named projimage (singular).

script/generate model projimage

projimage.rb

class Projimage < ActiveRecord::Base

has_attachment :content_type => :image,
:storage => :file_system,
:max_size => 5.megabytes,
:resize_to => '620x400>',
:thumbnails => { :thumb => '100x100>' }

###SPECIAL NOTE: windows does not pass the image size with the file to attachment_fu.  
###You can find the fix here: http://epirsch.blogspot.com/2008/01/fixing-attachmentfu-on-windows-like.html
###Or, you can leave the follwoing line commented and try the fix at a later time.  attachment_fu will still work.
#validates_as_attachment

end

Step 6 - create folder to store uploaded images:


attachment_fu defaults to storing files in public/<table name>
(in our case, the table name is projimages)
So, create a folder called projimages within the public/ directory
public/projimages

Step 7 - generate a migration named create_projimages.

script/generate migration create_projimages
Edit the migration (found in the db/migrate directory), and then perform a rake db:migrate.

class CreateProjimages < ActiveRecord::Migration

def self.up
create_table :projimages do |t|
t.column :parent_id,  :integer
t.column :content_type, :string
t.column :filename, :string    
t.column :thumbnail, :string
t.column :size, :integer
t.column :width, :integer
t.column :height, :integer
end
end

def self.down
drop_table :projimages
end
end


Step 8 - add the following to your config/routes.rb file in the config directory:

map.resources :projimages

You should now be able to navigate to the form to upload a picture (replacing 127.0.0.1:3009 with your local server, or web address)
http://127.0.0.1:3009/projimages/new

After uploading you should be taken to a page with just the thumbnail, clickable to see the full image.

You can see all of your thumbnails uploaded, and click to view the full image, by navigating here:
http://127.0.0.1:3009/projimages/

If you are trying to do multiple file uploads you may find this blog helpful: http://www.davesouth.org/stories/multiple-uploa...

I'm still in the process of getting it to work myself (it appears to require ImageScience, but we'll see if it works with Rmagick).
 
Comments (2)
2 Sunday, 25 March 2012 18:32
WallAnnette23
A lot of specialists say that credit loans aid people to live the way they want, just because they can feel free to buy needed things. Moreover, a lot of banks offer consolidation loan for young and old people.
1 Thursday, 03 September 2009 10:35
dharin
hi!
i want to restrict user to upload only video. audio and image files.
what should i write in :content_type => ???
Friday, 18 September 2009 21:03
Nelson
Try something like,

:content_type => ['video/x-flv', 'flv-application/octet-stream', 'application/octet-stream']

Add your comment

Your name:
Your email:
Your website:
Comment:
  The word for verification. Lowercase letters only with no spaces.
Word verification: