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
probably because the parameter is passed as a string instead of as a boolean.
Correct. The way I handle this in generic Ruby way is as follows:
class String
def to_b()
self.downcase == "true"
end
end
Now any string will have the to_b
method. You can you write
def ready?
if params[:ready].to_b
do_something
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.