Skip to content
gusgollings edited this page Sep 12, 2010 · 4 revisions

Rails 2 has amazing support for format driven responses. Given a photo object, by default it would have an HTML view that describes information about that photo. With Fleximage, the JPG or (GIF or PNG) view can be the image data itself.

A photo HTML view may look like this:

# app/views/photos/show.html.erb
# Accessed via http://mysite.com/photos/123

<p>
  <%= image_tag formatted_photo_path(@photo, :jpg) %>
</p>
<p>
  <b>Name:</b>
  <%=h @photo.name %>
</p>
<p>
  <b>Author:</b>
  <%=h @photo.author %>
</p>

That image tag uses a Rails route as its src. In this case, that route corresponds to the .jpg format of the photo resource, which would give us a URL like:

http://mysite.com/photos/123.jpg

This is the URL where the image will be.

You may also need to update your controller’s show method, passing jpg to the respond_to block:

# app/controllers/photos_controller.rb
  ...
  def show
    @photo = Photo.find(params[:id])

    respond_to do |format|
      format.jpg   # show.jpg.flexi (http://mysite.com/photos/123.jpg)
      format.html # show.html.erb
      format.xml  { render :xml => @photo }
    end
  end
  ...

Next: Rendering

Clone this wiki locally