Ruby on Rails query

doh

user
Joined
May 17, 2001
Messages
8,635
In my database I have a Films table with three columns: id, title, added_at.

In the controller for the list action I just do:

Code:
def list
  @films = Film.find(:all)
end

In the simplified list.rhtml view I'm trying to get at a list of attributes (e.g., columns) to print a table with:

Code:
<%= h @films.each { |k| k.inspect } %>

@films.class is an Array

What I did to make that work is:

Code:
<% @films.each { |f| %>
  <%= h f.inspect  %>
<% } %>

I don't understand why the this works and the one before it doesn't.

Output when these are together is:
Code:
#<Film:0x4ae37610> 
#<Film:0x4ae37610 @attributes={"title"=>"Ocean's 11", "id"=>"1", "added_at"=>nil}>

Could a Rails user point out the error of my ways? Why does
Code:
<%= h @films.each { |k| k.inspect } %>
print #<Film:0x4ae37610> ?
 
Okay I have figured it out...

Code:
Array.each { block }
returns an Array. So when I'm doing:

Code:
<%= h @films.each { |k| k.inspect } %>

What I'm effectively doing is:

Code:
<%= h @films %>

It seems the best way to do it is to split up the processing, nonprinting <% for the .each part and printing <%= for the block.


Next up: Find out why Rails isn't printing the contents of the block.
 
Back
Top