def my_controller @variable = [{''myfield''=>''grey''},{''myfield''=>''blue''}] end #-------------------------my_view <table> <%= render :partial=>''my_partial, :collection=>@variable %> </table> #----------------------_my_partial <tr> <td><%= my_partial[''myfield''] %> </td> </tr> I would have expected the view to print out grey, blue. However the my_partial local variable in _my_partial comes out a nil object. Can anyone tell what i''m doing wrong? thanks, simon. -- Posted via http://www.ruby-forum.com/.
You''ve set up @variable as an array of hashes and are trying to access it in your partial as a simple hash. You need to make your mind up whether you want an array, a hash, or something more complex. Simon wrote:> def my_controller > @variable = [{''myfield''=>''grey''},{''myfield''=>''blue''}] > end > > #-------------------------my_view > <table> > <%= render :partial=>''my_partial, :collection=>@variable %> > </table> > > > #----------------------_my_partial > <tr> > <td><%= my_partial[''myfield''] %> > </td> > </tr> > > I would have expected the view to print out grey, blue. > However the my_partial local variable in _my_partial comes out a nil > object. > > Can anyone tell what i''m doing wrong? > thanks, simon.-- Posted via http://www.ruby-forum.com/.
thanks for the reply, i''d like to access an array of hashes from my partial. Can i do this using the collection parameter? thanks. -- Posted via http://www.ruby-forum.com/.
D''oh! My mistake - what you are doing is fine. Your problem may be due to a missing quote in your call to render :partial. HTH, Mick. Simon wrote:> thanks for the reply, > i''d like to access an array of hashes from my partial. Can i > do this using the collection parameter? > thanks.-- Posted via http://www.ruby-forum.com/.
My understanding of the render :partial syntax is that when you use the :collection part, you pass an array to that argument and each element in that array is passed to the parial called (my_partial) as a local variable partial_name (my_partial) So at this point, if you have an array of simple hashes then each time my_partial runs it should run with a simple hash called my_partial If my understanding is correct, then I think the code should run. Can anyone clarify why this doesn''t work On 5/8/06, Mick Sharpe <mick.sharpe@btinternet.com> wrote:> You''ve set up @variable as an array of hashes and are trying to access > it in your partial as a simple hash. You need to make your mind up > whether you want an array, a hash, or something more complex. > > > Simon wrote: > > def my_controller > > @variable = [{''myfield''=>''grey''},{''myfield''=>''blue''}] > > end > > > > #-------------------------my_view > > <table> > > <%= render :partial=>''my_partial, :collection=>@variable %> > > </table> > > > > > > #----------------------_my_partial > > <tr> > > <td><%= my_partial[''myfield''] %> > > </td> > > </tr> > > > > I would have expected the view to print out grey, blue. > > However the my_partial local variable in _my_partial comes out a nil > > object. > > > > Can anyone tell what i''m doing wrong? > > thanks, simon. > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
<%= render :partial=>''my_partial'', :collection=>@variable %>> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org]On Behalf Of Mick Sharpe > Sent: Monday, 8 May 2006 10:49 AM > To: rails@lists.rubyonrails.org > Subject: [Rails] Re: partial problem > > > D''oh! My mistake - what you are doing is fine. Your problem > may be due > to a missing quote in your call to render :partial. > > HTH, Mick. > > > Simon wrote: > > thanks for the reply, > > i''d like to access an array of hashes from my partial. Can i > > do this using the collection parameter? > > thanks. > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Mick Sharpe wrote:> D''oh! My mistake - what you are doing is fine. Your problem may be due > to a missing quote in your call to render :partial. > > HTH, Mick.no my syntax looks fine - in my actual code anyway. -- Posted via http://www.ruby-forum.com/.
Is there definitely data in the @variable before you call the partial? Could you post your actual code? Cheers On 5/8/06, Simon <simon@vouchme.com> wrote:> Mick Sharpe wrote: > > D''oh! My mistake - what you are doing is fine. Your problem may be due > > to a missing quote in your call to render :partial. > > > > HTH, Mick. > > no my syntax looks fine - in my actual code anyway. > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Daniel ----- wrote:> Is there definitely data in the @variable before you call the partial? > > Could you post your actual code? > > CheersYeah thought of that, the @variable has the array of hashes in it at the view stage. The code above is exactly what I have test(with no missing quote). -Si. -- Posted via http://www.ruby-forum.com/.
So if in your view you put in an iterator instead of the call to partial (Not permenantly, just for debugging this one thing!) ie render :text => @variable.collect { |v| "<p>my field = #{p[''myfield'']}</p>" } Do you get any sensible output for this? On 5/8/06, Simon <simon@vouchme.com> wrote:> Daniel ----- wrote: > > Is there definitely data in the @variable before you call the partial? > > > > Could you post your actual code? > > > > Cheers > > Yeah thought of that, the @variable has the array of hashes in it at the > view stage. The code above is exactly what I have test(with no missing > quote). > > -Si. > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
yeah i looped through the array, except i used a for loop like <% for var in @variable %> <%= var[''myfield''] %> <% end %> -The output looks like what i expected. Are you getting the same results? I''d just like to know if i''m doing something wrong. -- Posted via http://www.ruby-forum.com/.
I''m at work at the moment, and I don''t have ruby installed on this box. In your partial have a look at what local and instance variables are available. ie. instance_variables.join(''<br/>'') local_variables.join(''<br/>'') Are the correct variables available? On 5/8/06, Simon <simon@vouchme.com> wrote:> yeah i looped through the array, except i used a for loop like > > <% for var in @variable %> > <%= var[''myfield''] %> > <% end %> > > -The output looks like what i expected. > > Are you getting the same results? I''d just like to know > if i''m doing something wrong. > > > > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
thanks Daniel, after display the local variables, it appears as though the keys of the hash are converted into variables directly. In this example, the local variable ''myfield'' becomes created in the partial as opposed to accessing it through my_partial[''myfield''] as i would have expected. The my_partial local variable then becomes a nil object. So much for the element of lease surprise :) -- Posted via http://www.ruby-forum.com/.
Thats a new one to me as well... Is that a bug? On 5/8/06, Simon <simon@vouchme.com> wrote:> thanks Daniel, > after display the local variables, it appears as though the keys of the > hash > are converted into variables directly. In this example, the local > variable ''myfield'' becomes created in the partial as opposed to > accessing it through my_partial[''myfield''] as i would have expected. > > The my_partial local variable then becomes a nil object. > > So much for the element of lease surprise :) > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
not sure if its a bug, it could be the way the render method is written internally. Its just not very transparent though. Daniel ----- wrote:> Thats a new one to me as well... > > Is that a bug?-- Posted via http://www.ruby-forum.com/.
There is a patch in the system for this. http://dev.rubyonrails.org/ticket/1557 On 5/8/06, Simon <simon@vouchme.com> wrote:> not sure if its a bug, it could be the way the render method > is written internally. Its just not very transparent though. > > Daniel ----- wrote: > > Thats a new one to me as well... > > > > Is that a bug? > > > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I''d say it''s definitely a bug. On 5/7/06, Simon <simon@vouchme.com> wrote:> > not sure if its a bug, it could be the way the render method > is written internally. Its just not very transparent though. > > Daniel ----- wrote: > > Thats a new one to me as well... > > > > Is that a bug? > > > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060516/b78729dc/attachment.html
Daniel ----- wrote:> There is a patch in the system for this. > > http://dev.rubyonrails.org/ticket/1557I ran into this problem, also. with an array of hashes being passed. It didn''t even seem to convert each has item into a local variable correctly (some didn''t exist, some were nil). Snippet below <% blah = [ {:id => "564788", :name => "test1"}, {:id => "3983dk", :name => "test2"}, {:id => "56eke8", :name => "test3"}, {:id => "ijks8", :name => "test4"} ] %> <%= render :partial => ''site_rows'' , :collection => blah %> The site_rows variable doesn''t show up as it is expected in the partial. site_rows[:id] and site_rows[:name} definitely don''t show up. Yes, I know this construct is pretty ugly (and I was just using it to test a partial and design), but it will be nice if it is fixed. Seems like this issue has come up a bunch of times in other posts (http://www.ruby-forum.com/topic/64890#74138 or http://www.ruby-forum.com/topic/64935#74295). The second post is actually comically sad since people seemed more interested in critiquing style than actually answering the guy''s question. Cheers -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---