Forum: Ruby on Rails controller if statement error with param

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.
Fididle F. (Guest)
on 2009-01-07 20:08
Hi, Please help me with this maddening little problem.

The if statement in the controller (below) does not detect the string
"ALL" contained in params[:state].  This param was entered in a form
(not shown here).

I can see on the rendered page (below) that this variable is indeed
present in the  '@state' variable as it is rendered correctly.

The question then is why the if '@state == "ALL"' condition is not
"true"? If it were true, @debug should be == 1.

I have also tried using a regex 'if @state =~ /ALL/' to no avail.

Thanks!

----------
controller
---------
...
def search
...
      @state  = params[:state]
      @debug = 0
    if @state  == "ALL"
        @debug = 1
    end
...
end

-----
view
-----

...

<p>DEBUG: @state = <%= @state %><br \>
   DEBUG: @debug = <%= @debug %><br \>
</p>
...


--------------
rendered page
--------------

DEBUG: @state = ALL
DEBUG: @debug = 0


Thanks!
Vishwanath N. (Guest)
on 2009-01-07 21:25
(Received via mailing list)
Hi,

are you sure its a string? just check with @state.class, and also try to
print the value before the if (so as to confirm its not getting modified
later on)

-NAYAK

On Wed, Jan 7, 2009 at 11:38 PM, Fididle Fididle <
Fididle F. (Guest)
on 2009-01-08 01:26
Vishwanath Nayak wrote:
> Hi,
>
> are you sure its a string? just check with @state.class, and also try to
> print the value before the if (so as to confirm its not getting modified
> later on)
>
> -NAYAK
>
> On Wed, Jan 7, 2009 at 11:38 PM, Fididle Fididle <

Thanks Nayak. I was trying to compare an array with a simple string.
Changing the test (below) works.

----------
controller
---------
...
def search
...
      @state  = params[:state]
      @debug = 0
    if @state[0]  == "ALL"
        @debug = 1
    end
...
end
This topic is locked and can not be replied to.