Okay, I’ve had some success rendering partials and felt I’d try
something more interesting with collections.
I have a query run by my model, which works. When I run it from the
console I can see all of the data I want. However, bringing that
into my view isn’t working the way I think. So obviously I’m
thinking wrong, but I can’t see where my error is. This usually
means I’m looking in the wrong place, so if someone can help point me
in the right direction I would appreciate it.
In my layout I have:
<%= render(:partial => “articles/synopsis”, :locals => { :synopsis =>
@article }, :collection => @article ) %>
Now if I understand correctly, that’s going to render the partial
“_synopsis.rhtml” from the articles view. Because the collection
uses a variable name based on the template, I’m setting that variable
to equal the variable coming from the current controller using
“:locals”.
In the template for the partial I have:
<%=h synopsis[:article][:title] %>
Which should print out the title of the article. The data is from a
relational query across two tables, so I think I need to specify the
table “:article” and the value I want from the array “:title” however
when I run that I get:
Symbol as array index
Looking at the other partials I’ve done, I thought maybe I shouldn’t
have “:title” as a symbol, so I changed my template to this:
<%=h synopsis[:article.title] %>
But that gives me a different error:
undefined method `title’ for :article:Symbol
If I use <%=debug(synopsis) %> in my template I can see all of the
data. So it seems obvious to me the data is there, I’m just not
accessing it correctly.
I know other people have to be doing this type of thing every day,
blindfolded and in their sleep. Some guidance on the error of
my ways would be appreciated.
Thanks!
Bill