Hi Cosmonaut and others,
Let's look at the problem:
<% for photo in @photos %>
<tr>
<% for column in Photo.content_columns %>
<% if column.name == "binary_data" %>
<%= image_tag("/photo_admin/code_image/#{@Photo.id}", :alt => "Image") %>
<% else %>
<%=h @Photo.send(column.name) %>
<% end %>
<% end %>
...
In this routine, we're looping through each photo record using the first line. Remember,
@photos contains our list of photos records (an
array of Photo objects if we want to be specific), and we want to loop through each of them and store the current record into the
photo variable so we can access it.
<% for photo in @photos %>
Therefore, when we want to get the id of the record, we need to use "photo" as our variable name. Not @photo, or Photo:
-- Photo (capitalized, and never with an @), is the class name and references the class
-- @photo (always lowercase, with an @ sign) is the instance variable. We set this in our controller. Instance variables are available in the view (our .rhtml files)
-- photo (always lowercase, without an @ sign) is a standard variable. That's the one we need, because we assigned the current photo record's data into it at the beginning of the loop (<%= for photo in @photos %>)
So now when we need to get the photo record's id, we use photo.id:
<%= image_tag("/photo_admin/code_image/#{photo.id}", :alt => "Image") %>Remember @photos is an array of Photo objects, each object representing a photo record and all of its data. We set this in the controller. Photo (capitalized) is the class name, and we don't usually reference that. We use it here to ask it about the columns it has (description, binary etc).
Hope this clarifies things a bit.
It's probably a little confusing using Photo, @photos and photo. It might be explained better if you think of:
<% for current_record in @photos %>
The "current_record" variable is then used to hold the current photo record. This is probably a bit clearer, although it's kind of convention to do it the other way, although more confusing for the beginner!
Last edited by mr_dizzy (2007-09-01 13:41:41)