Rendering jpeg from database blob

I have been doing some searching on the list and have found a potential
solution for displaying images from blob data, however, I am having some
issues getting it work.

My jpeg data is stored in a blob field in the same table as a person’s
information so using one of the available plugins may be difficult.

Here is my version of the suggested code:

(controller)
def student_image
@student = Student.find_by_id(params[:id])
@image = @student.id_picture
@filename = @student.first_name + “.jpg”
send_data @image, :filename => @filename,
:content_type => “image/jpeg”,
:disposition => ‘inline’
end

(view)
<%= image_tag url_for(:action => ‘student_photo’, :id => student) %>

When I try to load the page the pictures don’t display. I looked at the
logs and it appears that rails is trying to load static content from the
public/images directory. (with the filename being the student’s id and
the file extension “.png”) As a result I get a number of Routing
errors. Is there something wrong with my approach?

Thanks,
Jake

get the plugin file column it so kick db storages a$$

Jake S. wrote:

I have been doing some searching on the list and have found a potential
solution for displaying images from blob data, however, I am having some
issues getting it work.

My jpeg data is stored in a blob field in the same table as a person’s
information so using one of the available plugins may be difficult.

Here is my version of the suggested code:

(controller)
def student_image
@student = Student.find_by_id(params[:id])
@image = @student.id_picture
@filename = @student.first_name + “.jpg”
send_data @image, :filename => @filename,
:content_type => “image/jpeg”,
:disposition => ‘inline’
end

(view)
<%= image_tag url_for(:action => ‘student_photo’, :id => student) %>

When I try to load the page the pictures don’t display. I looked at the
logs and it appears that rails is trying to load static content from the
public/images directory. (with the filename being the student’s id and
the file extension “.png”) As a result I get a number of Routing
errors. Is there something wrong with my approach?

Thanks,
Jake

hey, i’m wondering about this line:
@image = @student.id_picture

is this @student.id_picture the same as @student.data?

Yes Jennifer, the “id_picture” field contains the blob data for a
students id badge photo.

-Jake

is the action name student_photo or student_image? maybe that’s the
issue?

Jennifer L. wrote:

(controller)
def student_image
@student = Student.find_by_id(params[:id])
@image = @student.id_picture
@filename = @student.first_name + “.jpg”
send_data @image, :filename => @filename,
:content_type => “image/jpeg”,
:disposition => ‘inline’
end

(view)
<%= image_tag url_for(:action => ‘student_photo’, :id => student) %>

hey, i’m wondering about this line:
@image = @student.id_picture

is this @student.id_picture the same as @student.data?

Yes Jennifer, the “id_picture” field contains the blob data for a
students id badge photo.

-Jake

Jennifer L. wrote:

Yes Jennifer, the “id_picture” field contains the blob data for a
students id badge photo.

-Jake

is the action name student_photo or student_image? maybe that’s the
issue?

Sorry, that was a typo in my post (should have cut/pasted). It should
read:

<%= image_tag url_for(:action => ‘student_image’, :id => student) %>

It seems like the image_tag helper is just taking the url that was
generated by url_for and just trying to serve that up as static content
rather than receiving the response from the send_data call.

The logs first show:
DEPRECATION WARNING: You’ve called image_path with a source that doesn’t
include an extension. In Rails 2.0, that will not result in .png
automatically being appended. So you should call
image_path(’/advisor/student_image/65757.png’)

and then the routing errors appear because the static content couldn’t
be found.

Jake S. wrote:

Jennifer L. wrote:

Yes Jennifer, the “id_picture” field contains the blob data for a
students id badge photo.

-Jake

is the action name student_photo or student_image? maybe that’s the
issue?

Sorry, that was a typo in my post (should have cut/pasted). It should
read:

<%= image_tag url_for(:action => ‘student_image’, :id => student) %>

It seems like the image_tag helper is just taking the url that was
generated by url_for and just trying to serve that up as static content
rather than receiving the response from the send_data call.

The logs first show:
DEPRECATION WARNING: You’ve called image_path with a source that doesn’t
include an extension. In Rails 2.0, that will not result in .png
automatically being appended. So you should call
image_path(’/advisor/student_image/65757.png’)

and then the routing errors appear because the static content couldn’t
be found.

i have almost the same thing working…now i’m wondering if it’s your
:id; should that be student.id? that’s working for me:
<%= image_tag(url_for(:action => ‘photo’, :id => photo.id), :border =>
0)%>

i have almost the same thing working…now i’m wondering if it’s your
:id; should that be student.id? that’s working for me:
<%= image_tag(url_for(:action => ‘photo’, :id => photo.id), :border =>
0)%>

I have tried a few more things:

Simplifying my student_image method to:

def student_image
@student = Student.find_by_id(params[:id])
send_data(@student.id_picture, :content_type => “image/jpeg”)
end

and disambiguating my view (to look more like yours, as well as changing
student to student.id)

<%= image_tag(url_for(:action => ‘student_image’, :id => student.id),
:border => 0) %>

I’m still getting the same results. The action doesn’t seem to be
getting called. Are you running Rails 1.2.1?

Jake S. wrote:

i have almost the same thing working…now i’m wondering if it’s your
:id; should that be student.id? that’s working for me:
<%= image_tag(url_for(:action => ‘photo’, :id => photo.id), :border =>
0)%>

I have tried a few more things:

Simplifying my student_image method to:

def student_image
@student = Student.find_by_id(params[:id])
send_data(@student.id_picture, :content_type => “image/jpeg”)
end

and disambiguating my view (to look more like yours, as well as changing
student to student.id)

<%= image_tag(url_for(:action => ‘student_image’, :id => student.id),
:border => 0) %>

I’m still getting the same results. The action doesn’t seem to be
getting called. Are you running Rails 1.2.1?

no, i’m not – not until i can overcome my daunting obstacles in 1.8!
one thing i forgot which is important. i’m doing these image tags in a
loop: for photo in @photo_collection (which is just a find(:all)).

so photo.id is not @photo.id. i’m sorry, i should have realized that in
my last post.

do you have any ideas for trying to send DOM objects to a controller as
parameters, like say a string array of ids? :wink:

no, i’m not – not until i can overcome my daunting obstacles in 1.8!
one thing i forgot which is important. i’m doing these image tags in a
loop: for photo in @photo_collection (which is just a find(:all)).

so photo.id is not @photo.id. i’m sorry, i should have realized that in
my last post.

do you have any ideas for trying to send DOM objects to a controller as
parameters, like say a string array of ids? :wink:

That’s exactly what I’m doing. I have a collection of student objects
who each have a photo. This means I have to do two lookups one to get
the student list and one to get their photo while looping through the
students. I’m still confused why I can’t get this to work.

I wish I had more options for dealing with the blob field, however, this
is getting propagated from outside of my rails app.

Has anybody else been able to use this to load blob data into a view (as
an image) with Rails 1.2.1?

Jake S. wrote:

no, i’m not – not until i can overcome my daunting obstacles in 1.8!
one thing i forgot which is important. i’m doing these image tags in a
loop: for photo in @photo_collection (which is just a find(:all)).

so photo.id is not @photo.id. i’m sorry, i should have realized that in
my last post.

do you have any ideas for trying to send DOM objects to a controller as
parameters, like say a string array of ids? :wink:

That’s exactly what I’m doing. I have a collection of student objects
who each have a photo. This means I have to do two lookups one to get
the student list and one to get their photo while looping through the
students. I’m still confused why I can’t get this to work.

I wish I had more options for dealing with the blob field, however, this
is getting propagated from outside of my rails app.

Has anybody else been able to use this to load blob data into a view (as
an image) with Rails 1.2.1?

hey best of luck – i’m pretty new to this. have you seen this guy’s
site? he imports images into db via fixtures & then displays them
:http://www.realityforge.org/articles/2006/04/21/eyelook-rails-photo-gallery

maybe he could/would be helpful

cheers

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