Forum: Ruby on Rails problem with local variable in partial template

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E7c538abb18858ae71b46a99158d13e7?d=identicon&s=25 harper.sean@gmail.com (Guest)
on 2007-02-22 06:58
(Received via mailing list)
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.
E7c538abb18858ae71b46a99158d13e7?d=identicon&s=25 harper.sean@gmail.com (Guest)
on 2007-02-22 15:35
(Received via mailing list)
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...@gmail.com" <harper.s...@gmail.com>
E7c538abb18858ae71b46a99158d13e7?d=identicon&s=25 harper.sean@gmail.com (Guest)
on 2007-02-22 15:41
(Received via mailing list)
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...@gmail.com" <harper.s...@gmail.com>
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (squeegy)
on 2007-02-22 22:44
harper.sean@gmail.com 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...@gmail.com" <harper.s...@gmail.com>

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}
This topic is locked and can not be replied to.