My Ruby on Rails function should receive a boolean parameter, check it and, if it is true, do something.
def isReady
if (params[:ready] == true)
doSomething()
end
end
However, with the example below we never get inside this if (but it enters the function), probably because the parameter is passed as a string instead of as a boolean. How can I pass boolean parameters properly, or convert them?
curl --data "ready=true" http://example.com/users/isReady
答案 0 :(得分:5)
可能是因为参数是作为字符串而不是布尔值传递的。
正确。我用泛型 Ruby方式处理它的方式如下:
class String
def to_b()
self.downcase == "true"
end
end
现在任何字符串都有to_b
方法。你可以写
def ready?
if params[:ready].to_b
do_something
end
end