RMagick: How to run scripts in rails

First off, cat.display probably won't work.

What you will need to do is something like this:

These go near the end of config/environment.rb:   require 'RMagick'   include Magick

These go in a controller action, say "show_cat":

def show_cat   cat = ImageList.new("Cheetah.jpg")   image = cat.first # first image frame in the image list   img.format = 'JPG' # force conversion to jpeg if needed

  send_data(img.to_blob, { :type => 'image/jpeg',                                :filename => 'Cheetah.jpg',                                :disposition => "inline" })


I believe that send_data is a "show stopper" in that it does not render a template once you use it. At least I don't have a show_cat.rhtml or other type of file for it to render. Note that this method could use some error checking to make certain that an image is actually loaded.

I also do this in the same controller, before calling send_data():

I don't think you understand how images work in html.

To display an image, you use the standard <img src="http://host/controller/action/id&quot; /> html tag. This calls your controller with the action you want. For instance, I have a "avatar_img_tag" helper action which generates image tags of the form:

  <img src="/avatar/show/1" />

for me.

When this html is processed by a browser, it will make another request to your rails code to retrieve the image itself.

That controller has an action, "show", which calls send_data. You cannot really use the image data directly in your view; you must use send_data to send the image data.


At that point, after the @image.write part (remove the @image = part of that, btw) you have written it to a file.

In your edit.rhtml file, you would say something like :

<img src="<% url_for :controller => :imageview, :action => show, :file => 'clown.jpg' %>"/>

and then in your controller, put something like the code I sent previously that calls send_data() or send_file().

Just be careful using pathnames -- remove all cases of .. from them or you'll be burned.

Note that one "hit" to your server CANNOT both display HTML AND IMAGE CONTENT. You have to do this in two steps.


Hi, I am trying to install RMAgick but just cant seem to get ImageMagick working. (ubuntu) Can you perhaps tell me what gems (including version numbers) you have installed and in which order. The actuall links to the gems you installed to get RMagick working would be a blessing.

Sorry for putting this question here, but you guy's probably all got this working with ease.

Thanks in Advance

Scott A S wrote: