FlexImage and respond_to

November 27th, 2006

This information is now old. A new version of the plugin can be found here with lots of instructions and examples.

TomFarm has posted a very cool example about how to use FlexImage with the respond_to(format) style process handling.

NOTE: this requires rails 1.2 or edge rails

This gives you a nice RESTful solution to image display simply by file extension. You can render an image with:

/project_images/1.jpg

Or display the html with a title and caption for an image with:

/project_images/1

Or get the image attributes in xml with:

/project_images/1.xml

Here’s how:

First, you have to register the jpg extension with the Rails MIME handler.

#environment.rb
Mime::Type.register("image/jpeg", :jpg)

Make sure you are using restful routes for you image urls.

#routes.rb
map.resources :project_images

And finally create your respond_to block.


# ProjectImagesController
def show
  @project_image = ProjectImages.find(params[:id])
  respond_to do |format|
    format.jpg  { render_flex_image(@project_image) }
    format.html # show.rhtml
    format.xml  { render(:xml => @project_image.to_xml) }
  end
end

Pretty slick. Thanks Tom!

7 Responses to “FlexImage and respond_to”

  1. Eric Says:

    First, thanks for your work.

    I installed flex image but I get an undefined method `exempt_from_layout’ error in init.rb

    What should I do for this problem? Because is really what fit to my project.

    Eric

  2. Daniel Butler Says:

    Alex, Eric,

    I encountered the same problem.

    Looks like some code that’s incompatible with Rails 1.1.x was introduced to init.rb in version 121. If you revert to 119, things should continue to work.

    Daniel


    Index: init.rb

    - init.rb (revision 119) + init.rb (revision 121) @ -4,5 +4,5 @ require ‘flex_image/model’ require ‘flex_image/view’

    -ActionController::Base.exempt_from_layout :rjpg -ActionView::Base.register_template_handler :rjpg, FlexImage::View::Handler +ActionController::Base.exempt_from_layout :flexi +ActionView::Base.register_template_handler :flexi, FlexImage::View::Handler

  3. Alex Wayne Says:

    Sorry about that, the plugin was briefly incompatible with rails 1.1.6, as an in-progress feature was checked in that require rails 1.2 or edge.

    Sorry about that, it’s all been fixed up now and should with 1.1.6 without a problem. So just re-install, up update your plugin and you should be dandy.

  4. craig Says:

    Does anybody know how to cleanly install FlexImage in the following setup (local development machine):

    Windows XP, RadRails, Subversion

    If I install via the command line ruby script/plugin install …, it works ok, but it gets Subversion in a state where it thinks the repository is out of sync. I.e. from RadRails it will not let you commit anything back to the repository.

    The hack way I found around this was to create a new temp project in RadRails, install FlexImage into this project via the command line, then goto Radrails & drag the FlexImage folder into my real project.

  5. Alex Wayne Says:

    if your project is under subversion you can make the plugin install script use svn:externals with the -x options. This places a reference to the plugin repository in your plugins folder.

    ruby script/plugin install -x http://beautifulpixel.com/svn/plugins/flex_image

    Then make sure you refresh your project directory in radrails, then simply commit. Should work fine as I do this myself with plugins all the time.

  6. Matthew King Says:

    The after_save :write_image_to_disk was causing me trouble when updating a flex_image record without uploading a file.

    I “fixed” my problems by redefining the write_image_to_disk method as below.
    def write_image_to_disk
        rmagick_image.write(file_path) if @rmagick_image && @@file_store    
    end
    

    I may be missing something obvious, though.

  7. Alex Wayne Says:

    I have just added the above code to the SVN. Thanks for the contribution!

Leave a Reply