Failure in comparisons

I’m basically a Ruby noobie.

At the moment I’m having a fair bit of trouble trying to figure out
what’s going wrong here (partial template):

<%=
@project = Project.find(@the_id)
if current_user.userid == @project.client
hidden_field ‘messages’, ‘recipient’, ‘value’ =>
@project.project_manager
else
hidden_field ‘messages’, ‘recipient’, ‘value’ => @project.client
end
%>

The current_user.userid method is as follows (from model user.rb):

def userid
“#{self.id}”
end

All the values I use there are correct. Say that the current user id is
2, you are the client (which == 2), and the project manager (id) is 3.
The form that is output is “hidden_field ‘messages’, ‘recipient’,
‘value’ => @project.client” instead of “hidden_field ‘messages’,
‘recipient’, ‘value’ => @project.client”, and it happens every time. Why
does it not work? I tried stripping white space from the userid, I even
looked for possible string comparison problems, but there is no
easy-to-find info online. Please help!

Hi Isaac,

Isaac Rowntree wrote:

 hidden_field 'messages', 'recipient', 'value' => @project.client

end
%>

For starters, I’d suggest you do the majority of the above, especially
the
model access (Project.find) in your controller. Second, even if you
decide
not to follow the MVC model, your current_user will not be available in
the
view unless you’re passing it in as a local variable to the partial. If
not, you’d need to make it an instance variable.

hth,
Bill

On 7/12/06, Bill W. [email protected] wrote:

if current_user.userid == @project.client
not to follow the MVC model, your current_user will not be available in
the
view unless you’re passing it in as a local variable to the partial. If
not, you’d need to make it an instance variable.

hth,
Bill

wouldn’t current_user be available if it’s a helper method? Like that
provided by acts_as_authenticated.

Yes, I should have done it in the controller (slaps wrist). However,
until I got it working I assumed it didn’t matter where it was - I just
wanted to figure out why it didn’t work. And no, it still doesn’t work.
The following code still fails to compare the two numbers together
correctly.

current_user.userid returns the value ‘2’
@project.client returns the value ‘2’
thus @recipient should equal ‘3’

however, it equals ‘2’ when I output the form (<%= hidden_field
‘messages’, ‘recipient’, ‘value’ => @recipient %>)

def reply_message
@project = Project.find(@the_id)
if current_user.userid == @project.client
@recipient = @project.project_manager
else
@recipient = @project.client
end
end

What’s going wrong here???

That’s what I thought, and that’s basically what I said in my original
post. However, I looked everywhere but couldn’t find any indication on
how to cast to an integer. I know how to cast to a string (to_s), but
yeah…

What is probably happening here is that you are comparing a string “2”
to an integer (2).

“2” == 2 #=> false

On Wednesday, July 12, 2006, at 4:04 AM, Isaac Rowntree wrote:

however, it equals ‘2’ when I output the form (<%= hidden_field

What’s going wrong here???


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

What is probably happening here is that you are comparing a string “2”
to an integer (2).

“2” == 2 #=> false

_Kevin

On Wednesday, July 12, 2006, at 4:27 AM, Isaac Rowntree wrote:


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

“2”.to_i => 2

_Kevin

Thanks mate - sigh ruby is too logical sometimes… :wink:

“2”.to_i => 2

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs