Serve an image up from an action

What I am really trying to do is use the file_column plugin for the file
uplaod/storage (to a non-application directory), but serve up the images
on my own (without the mage_tag url_for_file_column technique)

In the java world, I would simply do something like this:

and myImageServlet would simply return the appropriate mime headers and
data that represented the image.

I am lost in the ruby/rails world.

My images are being stored in a NON application folder (I do not want
users to be able to access them directly…I want the retrieval of the
images to be controlled by an action.

I have an action in one of my controllers that looks something like
this:

def serve_user_image
@user = session[:user]
image_file = File.new(@user.image_filename, “rb”)
send_data image_file, :filename => @user.image_filename, :type =>
“image/jpeg”, :disposition => “inline”
end

And inside my view, something like this:

<%= image_tag url_for(:action => “serve_user_image” ) %>

However, aside from it not working, I get really weird things in my logs
like
"Parameters: {“action”=>“serve_user_image.png”, “controller”=>“main”}

Now, there is no reference to a png in my code…(the filename is a
.jpg), so I assume I am doing something dreadfuly wrong.

Any pointers would be appreciated

Anyone? I am sure there is a ruby-eque way of doing this, I just can’t
seem to get it right…

Thanks…

You might look at:

http://wiki.rubyonrails.com/rails/pages/HowtoUploadFiles

which talks about reading/writing images to a database. Should be
modifiable
to the file system using IO classes.

You’re getting .png because Rails is interpreting your url_for params as
a
relative url with no file extension, .png is the default.

HTH,
Jeff

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs