Question

Problem displaying image from database I am working on a website which allows both customers and account users to upload their pictures.

Using a recipe provided in the book Agile Web Development with Rails, 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 => @customer.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
  end

Notice the singular basic_profile which makes the path as

//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 => @basic_profile.id) %>" alt="Picture" class="profile_picture" />

I have tried to fix this but nothing works. Senselessly playing with the path like

//localhost:3000/accounts/1/basic_profile/picture

gives me routing error.

Please help.

Was it helpful?

Solution

Solved!

The solution was simple. Add "get :picture" within the nested route.

  resources :accounts do
    resource :basic_profile do
      get :picture
    end
  end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top