I have what seems a common task: 1. In response to a user request, look for a thumbnail of a product. 2. If no thumbnail, generate one using RMagick and store in database. 3. In either case render bits to browser, either from blob in database or computed thumbnail This problem seems to straddle the model and controller patterns. It occurred to me that a helper could handle it, but I would prefer not to use one, as an attribute seems so much cleaner. What I would like is: <img src="<%= product.faux_thumbnail %>" /> And all the magic in steps 1..3 happen. Any suggestion where in the application structure to place this logic (send_data only seems to work in controllers...). Thanks
You need a separate action for the image. product.faux_thumbnail would 
create a link to /controller/image/1234
Then you have an action called "image" that generates the image and
sends it
with send_data.
Don''t know if that''s what you''re asking, seems pretty
straightforward to do.
Here''s an example of a game I made:
def board
    board = Image.new( 306, 306, HatchFill.new( ''white'',
''black'', 51 ) )
    board.border!( 1, 1, ''black'' )
    send_data(board.to_blob { self.format = ''GIF'' },
              :filename => ''board.gif'',
              :type => ''image/gif'',
              :disposition => ''inline'')
  end
<img src="<%= url_for :controller => ''game'',
:action => ''board'' %>" id="b"/>
-Jeff
----- Original Message ----- 
From: "Steve Ross"
<sross-ju+vs0qJmycyYsO2DCxJlVaTQe2KTcn/@public.gmane.org>
To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org>
Sent: Friday, July 01, 2005 2:55 PM
Subject: [Rails] Dynamic Image Display
>I have what seems a common task:
>
> 1. In response to a user request, look for a thumbnail of a product.
> 2. If no thumbnail, generate one using RMagick and store in database.
> 3. In either case render bits to browser, either from blob in database or
> computed thumbnail
>
> This problem seems to straddle the model and controller patterns. It
> occurred to me that a helper could handle it, but I would prefer not to 
> use
> one, as an attribute seems so much cleaner. What I would like is:
>
> <img src="<%= product.faux_thumbnail %>" />
>
> And all the magic in steps 1..3 happen.
>
> Any suggestion where in the application structure to place this logic
> (send_data only seems to work in controllers...).
>
> Thanks
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
"Steve Ross" <sross-ju+vs0qJmycyYsO2DCxJlVaTQe2KTcn/@public.gmane.org> writes:> This problem seems to straddle the model and controller patterns. It > occurred to me that a helper could handle it, but I would prefer not to use > one, as an attribute seems so much cleaner. What I would like is: > > <img src="<%= product.faux_thumbnail %>" />I''ve just gone through this for user avatars via photos. I started with the assumption i wasn''t going to store the image in the db, but on the filesystem. Anyway, that doesn''t matter. I have methods on the model for things like the image''s url and it''s img tag. So I have code that does: <%= @user.image.tag %> for the full-sized image and <%= @user.image.thumbnail.tag %> for the thumbnail. The controller just has to load the image. The image knows how to display itself. The image knows how to generate or load it''s own thumbnail. I did some write up on this today on my blog; not much details, but some concepts: http://lathi.net/diary/Software/RubyOnRails/image_handling_in_ruby_on_rails.html -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org