small medium large xlarge

27 Apr 2013, 04:37
Kingston Sew Eng Hock (7 posts)

Problem displaying image from database

Hi, I am working on a website which allows both customers and account users to upload their pictures.

Using the recipe provided(in topic above), I have managed to make it work for the customers module where I have two tables, Customer and Image on a 1 on 1 relationship. And I display the image with

<img src="<%= url_for(:action => 'picture', :id => %>" alt="Picture" class="photo"/>

which generates the following html example

<img src="/customers/picture/986" alt="Picture" class="photo">

Now, my second module is slightly more complicated where I have 3 tables involved. 1. Account - which only stores username and password 2. Basic_profile - which stores things like Name, Gender, birthday and etc. Note - I’m using the edit page here to upload and display the user’s picture. 3. Picture - which stores account users pictures.

The relationship here is Picture belongs_to Basic_profile, and Basic_profile belongs_to Account.

In my routes I have a nested resource.

  resources :accounts do
    resource :basic_profile, :contact

Notice the singular basic_profile which makes the path as http://localhost:3000/accounts/1/basic_profile/edit

Uploading and saving into the database are all fine. My problem now is displaying the picture in the edit page. The following code doesn’t work.

<img src="<%= url_for(:action => 'picture', :id => %>" alt="Picture" class="profile_picture" />

I have tried to fix this but nothing works. Senselessly playing with the path like http://localhost:3000/accounts/1/basic_profile/picture gives me routing error.

Please help. Thanks!

29 Apr 2013, 08:43
Kingston Sew Eng Hock (7 posts)

Anybody? Please help. Is it something to do with my routes?

01 May 2013, 04:48
Kingston Sew Eng Hock (7 posts)

Dear Sam,

I am still stuck. Really hoping you or someone else could shed some light. If my question is out of topic, please at least give me a pointer on where to get help.


01 May 2013, 12:41
Sam Ruby (584 posts)

This forum is for help with the book.

It is not clear to me what you are attempting to do. Taking a guess, what you want is

resources :accounts do
  resource :basic_profile do
    get :picture


<img src="<%= picture_account_basic_profile_path( %>" alt="Picture" class="profile_picture" />
02 May 2013, 04:50
Kingston Sew Eng Hock (7 posts)

I’ve completed part 2 of the book and trying to go through part 3. I still have a lot to learn.

Yes you guess right. I was trying to display the picture but just couldn’t get it right. The ` get :picture ` was what I needed. Curious how come I didn’t need to get picture on the customers part? All I needed was ` resources :customers `

Anyway it is working now. Thank you so much!

  You must be logged in to comment