Passing size for image_tag to view via model

Hi group,
I have a problem with an image_tag. It’s rather complicated and I
can’t find an easy solution.

I have a model called “Medium” which can hold a paperclip file
attachment or a remote url. I want to treat all media equally from my
view, so if I need a preview picture, I use get_picture(size). This
matches with paperclip’s autogenerated thumbnails and everything is
fine.

In case I have a youtube-video in my remote link, things are starting
to get ugly. Fortunately youtube autogenerates preview pictures from
the videos, and I am totally okay with these, the only problem is that
I can’t resize them properly without busting my view.

So what I need is a possibility to say my get_picture(size)-method
something like

case size
when :large
return youtube_picture.png, size => “1000x1000”
when :small
return youtube_picture.png, size => “5x5”
end

From the view, I want to call

<%= image_tag medium.get_picture(:small) %>
and I don’t want to pass in the parameter on view side, because that
would be very repetitive.

My workaround was to make a method “get_size(size)”, so that I can use
<%= image_tag medium.get_picture:small, :size =>
medium.get_size(:small) %>
but that’s ugly and there has to be a better way to do this.

Is there any?

Thanks in advance!

How about something like this:

attr_reader :size

Sizes = {:small => ‘5x5’
:large => ‘1000x1000’}

def get_preview(size)


end

def small_preview
@size = Sizes[:small]
@small ||= get_preview(@size)
end

def large_preview
@size = Sizes[:large]
@large ||= get_preview(@size)
end

<%= image_tag medium.small_preview, :size => medium.size %>

…but doesn’t that need to be @medium?

I don’t know enough about rails to say no. But I also don’t understand
what you are hoping for.

Hi,

if I call image_tag without the :size param, it can happen that, if I
have images of different sizes, they bust my view because they all are
of different sizes. If i call :size => “xxy” in every line of the code
where I want a picture to be force-resized, i repeat myself over and
over, and in the second I get the idea of changing one picture’s
dimension, I’m in trouble.

So what I want is my view to say to my model “hi, i want your image
tag” and my model to say “here it is, oh, btw, it has a fixed
resolution of 250x150 px, display this, kkthxbb”

Hope I made it clearer now? :slight_smile:

Thanks for your help anyway

On 3 August 2011 17:21, manavortex [email protected] wrote:

In case I have a youtube-video in my remote link, things are starting
when :small
<%= image_tag medium.get_picture:small, :size =>
medium.get_size(:small) %>
but that’s ugly and there has to be a better way to do this.

Use a view helper to replace that code with a call to your helper
<%= auto_size_image_tag( medium ) %>

Colin

Hi,
thanks for your reply!

<%= image_tag medium.small_preview, :size => medium.size %>

Yeah, that is the solution I found, but I don’t think it’s pretty.
Isn’t there a possibility to make this better, like link_to
method_returnvalue or something?

If you say no, I will cry :slight_smile: