Ruby - ERROR_Can't convert nil into an exact number - ruby-on-rails

I try to make that, in the controller ...
raw_counter_reset_date = REDIS.hget(params[:id], 'overquota_reset_at')
#counter_reset_date = Time.at(raw_counter_reset_date)
But when I want lauch application, I have this error :
can't convert nil into an exact number
I know that #counter_reset_date = Time.at(1364046539) works with numbers, but I would that the application take the timestamp date in a database and convert in date on my web-page.
I hope to have been understandable, and thank you in advance for your help.

this is because raw_counter_reset_date is nil. convert it to a number (raw_counter_reset_date.to_i) or check for nil.

Related

Convert UTF string to Chinese Language from JSON response

I tried everything to convert JSON response to Chinese language but not getting any success. I need to display those string in uilabel.
This is the response I'm getting:
sentence = "\U00e6\U201a\U00a8\U00e5\U00a5\U00bd\U00e3\U20ac\U201a";
pinyin = "n\U00c3\U00adn h\U00c4\U0192o"
Converting sentence's string should be like 您好 but I'm getting 您好。
For pinyin I'm getting exactly right string [[nín hăo]] in label without converting but for sentence it gives me wrong value.
I'm using XCode 7.1 and my deployment target is 8.0.
Hi thanks all for helping and trying :) i ended up solving my own problem. What I did is directly put dict value to label text rather than passing from NSString. Taking it into string will give me value like 您好。
Here is what i've done.
cell.lblWord.text = [NSString stringWithFormat:#"Word: %#",[[dic objectForKey:#"cat"]objectForKey:#"chart"]];
It's strange but true, tried before but wasn't working.

Array.size() returned wrong values (Grails)

I'm developing an app using Grails. I want to get length of array.
I got a wrong value. Here is my code,
def Medias = params.medias
println params.medias // I got [37, 40]
println params.medias.size() // I got 7 but it should be 2
What I did wrong ?
Thanks for help.
What is params.medias (where is it being set)?
If Grials is treating it as a string, then using size() will return the length of the string, rather than an array.
Does:
println params.medias.length
also return 7?
You can check what Grails thinks an object is by using the assert keyword.
If it is indeed a string, you can try the following code to convert it into an array:
def mediasArray = Eval.me(params.medias)
println mediasArray.size()
The downside of this is that Eval presents the possibility of unwanted code execution if the params.medias is provided by an end user, or can be maliciously modified outside of your compiled code.
A good snippet on the "evil (or lack thereof) of eval" is here if you're interested (not mine):
https://javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval/
I think 7 is result of length of the string : "[37,40]"
Seems your media variable is an array not a collection
Try : params.medias.length
Thanks to everyone. I've found my mistake
First of all, I sent an array from client and my params.medias returned null,so I converted it to string but it is a wrong way.
Finally, I sent and array from client as array and in the grails, I got a params by
params."medias[]"
List medias = params.list('medias')
Documentation: http://grails.github.io/grails-doc/latest/guide/single.html#typeConverters

How can I remove the quotation mark and commas?

I have a date in "12/5/24" format and I am trying to use it in a JavaScript function.
How can I use the date with no quotation marks and with commas?:
Date.UTC(12,5,24)
This is what I am trying to achieve:
date = "12/5/24"
var = date.some_method #=> 12,5,24
Date.UTC(var)
Write as below :
string = "12/5/24"
Date.UTC(*string.split('/').map(&:to_i))
date_array = "12/5/24".split("/").collect(&:to_i)
Date.UTC(date_array[0], date_array[1], date_array[2])
All the split and gsub answers are "wrong", because they fit the anti-pattern "cause the bug, then fix the bug." (Yet they are not "completely wrong", because sometimes that anti-pattern is unfortunately the simplest option!)
Use Time.parse(date).strftime('%m,%d,%y'). That's why you have to escape each y token in a time template with a percent % - so you have a whole string to play with, and can put anything else in it. You could even put the rest of your Javascript in there.
Date.UTC( *"12/5/24".split('/') )

split the string to get specific value on ruby on rails?

I want to get two separate value from
/en/faq
the two separate value will be
lang =en
rem =faq
I have used to split which was relatively much easier. Nonetheless, this is my approach which needs tweaking around, hopefully from your help I will be able to accomplish it.
string = "/en/faq"
lang = string.split("/").first
rem = string.split("/en/")
puts "/#{lang}/#{rem[1]}"
The desired output should be "/en/faq/" but the output is
"//faq"
i know i have got '.first' which is why I am getting a null value but could anyone help on getting the right results please?
thanks in advance.
string = "/en/faq"
lang = string.split("/")
rem = string.split("/#{lang[1]}/")
puts "/#{lang[1]}/#{rem[1]}"
this does the trick and thanks to Sebi for his prompt answer!

Rails: My code causing "can't convert nil into String"

This code from my developer below... does it expect something like "1 - January" and try to parse it into something else?
I changed the code on my form so now the value being passed to this controller is just "1" instead of "1 - January"
How can I fix this so I don't get the "can't convert nil into string" error?
def get_expiry_month_number(monty_det, expiry_year)
if MONTH_NAMES.include? monty_det
month_number = MONTH_NAMES.index(monty_det)
month_number = MONTH_NUMBERS[month_number]
expiry_year = month_number.to_s + expiry_year[expiry_year.length-3..expiry_year.length-1]
return expiry_year
end
end
In your example you are running into the problem that one of your strings in your calculation for year is nil. Most likely this one,
expiry_year[expiry_year.length-3..expiry_year.length-1]
You can try and fix this by calling to_s on the culprit variable, thus converting the nil into '' and then concatenated.
Like so: expiry_year = month_number.to_s + expiry_year[expiry_year.length-3..expiry_year.length-1].to_s
However, this may have other seen consequences and isn't recommended. A better approach would be to find out why something you expect is a string is turning out to be empty, and perhaps displaying an appropriate error.
Could you explain a little bit more about what the function is supposed to do? And perhaps the value of expiry_year and monty_det when the error occurs? puts statements can be a big help here.
I guess that get_expiry_month_number('january', 2011) would return: 1011.
Indeed:
month_number should return 1 for january
expiry_year[expiry_year.length-3..expiry_year.length-1] returns the 3 last characters of 2011 => 011

Resources