Passing Boolean Parameters in Ruby on Rails

The My Ruby on Rails function should get a boolean parameter, check it, and if it's true, do something.

  def isReady
     if (params[:ready] == true)
            doSomething()
     end
  end

      

However, in the example below, we never get inside this if (but it is part of the function), probably because the parameter is passed as a string and not as a boolean. How can I properly pass boolean parameters or convert them?

curl --data "ready=true" http://example.com/users/isReady

      

+3


source to share


1 answer


perhaps because the parameter is passed as a string and not as a boolean.

Right. The way I handle it in ruby ​​way is as follows:

class String
  def to_b()
    self.downcase == "true"
  end
end

      



Now any string will have a method to_b

. You can write

def ready?
  if params[:ready].to_b
    do_something
  end
end

      

+5


source







All Articles