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
On Mon, 2006-03-20 at 10:50 -0700, Bill Wilcox wrote:> 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---- I think that is telling you that you are using [:article] symbol as an array index but it is not. Craig
On Mar 20, 2006, at 10:50 AM, Bill Wilcox wrote:> 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.It may be bad form to reply to oneself, but I guess I need confirmation that I''m thinking correctly now. When I used ":local" to assign the values of "@article" to "synopsis", did I effectively "flatten" my query results into an array instead of keeping them in a hash? So when I then tried to access those values using the hash id, it gave me an error. I redid my partial to exclude the "local" object, so it now looks like this: <%= render(:partial => "articles/synopsis", :collection => @article ) %> Now in my template I can use the following and it works exactly as I would expect: <%=h synopsis.title %><br> <%=h synopsis.teaser %><br> <%=h synopsis.category.name %><br> The API doc is a little confusing to me though. "Two controllers can share a set of partials and render them like this: <%= render :partial => "advertisement/ad", :locals => { :ad => @advertisement } %> This will render the partial "advertisement/_ad.rhtml" regardless of which controller this is being called from. " If I code the way I read that, it doesn''t work. So I think my understanding of the above is where my flaw is. In what case would I want to use ":locals" to assign a variable to the local variable? Thanks! Bill