Hiya,
A simple problem I expect, but I cannot seem to find a simple way to do
it.
How can I check to see if a parameter is a number or not. Bear in mind
it may be nil so I need to cope with that situation too.
Thanks,
~ Mark
Hiya,
A simple problem I expect, but I cannot seem to find a simple way to do
it.
How can I check to see if a parameter is a number or not. Bear in mind
it may be nil so I need to cope with that situation too.
Thanks,
~ Mark
Sorry, I should clarify that I mean a querystring parameter. As in:
(in a controller method)
params[:some_key]
if params[:some_key].to_i != 0
Granted, you’ll need a separate check if the number actually can be 0,
but
String#to_i returns 0 when there is no number.
Jason
Hi Mark,
Mark D. wrote:
How can I check to see if a parameter is a number or
not. Bear in mind it may be nil so I need to cope with
that situation too.
In your model…
validates_numericality_of :some_key
Check the doc at api.rubyonrails.org for a number of options.
hth,
Bill
Stefan M. wrote:
Jason R. wrote:
if params[:some_key].to_i != 0
Granted, you’ll need a separate check if the number actually can be 0, but
String#to_i returns 0 when there is no number.Depending on the OPs needs he may want to use the method Integer and
rescue the ArgumentError - or just use Bills suggestion.
you can also try and match via regexp
params[:some_key] =~ /^\d+$/
Jason R. wrote:
if params[:some_key].to_i != 0
Granted, you’ll need a separate check if the number actually can be 0, but
String#to_i returns 0 when there is no number.
Depending on the OPs needs he may want to use the method Integer and
rescue the ArgumentError - or just use Bills suggestion.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs