harper.sean-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Feb-22 05:57 UTC
problem with local variable in partial template
I am quite new at Ruby and Rails and this is stumping me: I have in my rhtml file: <ul> <%= render(:partial => "owner", :collection => @business.owners, :locals => { :that_value=> @params[:that_value] } ) %> </ul> then in my partial: <% if owner.id == that_value %> <li><%= owner.name %> <%= owner.id %> <%= that_value%> </li> <% else %> <li><%= owner.name %> <%= owner.id %> </li> <% end %> <%= owner.id == that_value%> <%=owner.id%> <%=that_value%> owner.id == that_value evaluates to false every time, even when they are really the same. Sample output: Owner Number1 1 False 1 2 Owner Number2 2 False 2 2 Owner Number3 3 False 3 2 I am an old C guy and I keep thinking that there must be some typing error, like one of the "2" is a string and the other is an int, but I thought ruby has no types. I am pretty stumped, hopefully someone can help. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
harper.sean-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Feb-22 14:34 UTC
Re: problem with local variable in partial template
And if I change <% if owner.id == that_value %> to <% if owner.id <> that_value %> it fails. On Feb 21, 11:57 pm, "harper.s...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <harper.s...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am quite new at Ruby and Rails and this is stumping me: > > I have in my rhtml file: > > <ul> > <%= render(:partial=> "owner", :collection => > @business.owners, :locals => { :that_value=> @params[:that_value] } ) > %> > </ul> > > then in mypartial: > > <% if owner.id == that_value %> > <li><%= owner.name %> <%= owner.id %> <%= that_value%> </li> > <% else %> > <li><%= owner.name %> <%= owner.id %> </li> > <% end %> > <%= owner.id == that_value%> > <%=owner.id%> <%=that_value%> > > owner.id == that_value evaluates to false every time, even when they > are really the same. > > Sample output: > > Owner Number1 1 > False > 1 2 > Owner Number2 2 > False > 2 2 > Owner Number3 3 > False > 3 2 > > I am an old C guy and I keep thinking that there must be some typing > error, like one of the "2" is a string and the other is an int, but I > thought ruby has no types. I am pretty stumped, hopefully someone can > help.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
harper.sean-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Feb-22 14:40 UTC
Re: problem with local variable in partial template
It''s really confusing because they both print out as being the same (in this case 2) but they don''t compare to be equal with each other... On Feb 22, 8:34 am, "harper.s...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <harper.s...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> And if I change <% if owner.id == that_value %> to <% if owner.id <> > that_value %> it fails. > > On Feb 21, 11:57 pm, "harper.s...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <harper.s...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > I am quite new at Ruby and Rails and this is stumping me: > > > I have in my rhtml file: > > > <ul> > > <%= render(:partial=> "owner", :collection => > > @business.owners, :locals => { :that_value=> @params[:that_value] } ) > > %> > > </ul> > > > then in mypartial: > > > <% if owner.id == that_value %> > > <li><%= owner.name %> <%= owner.id %> <%= that_value%> </li> > > <% else %> > > <li><%= owner.name %> <%= owner.id %> </li> > > <% end %> > > <%= owner.id == that_value%> > > <%=owner.id%> <%=that_value%> > > > owner.id == that_value evaluates to false every time, even when they > > are really the same. > > > Sample output: > > > Owner Number1 1 > > False > > 1 2 > > Owner Number2 2 > > False > > 2 2 > > Owner Number3 3 > > False > > 3 2 > > > I am an old C guy and I keep thinking that there must be some typing > > error, like one of the "2" is a string and the other is an int, but I > > thought ruby has no types. I am pretty stumped, hopefully someone can > > help.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
harper.sean-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> It''s really confusing because they both print out as being the same > (in this case 2) but they don''t compare to be equal with each other... > > On Feb 22, 8:34 am, "harper.s...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <harper.s...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>Your most likely comparing a string to an integer 1 == "1" #=> false All params come in as strings. If you want it to be treated as an integer, you can convert it easily. 1 == "1".to_i #=> true So in your locals hash you can do: :locals => {:that_value => params[:that_value].to_i} -- 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 -~----------~----~----~----~------~----~------~--~---