I have a cookies.permanent[:liked]:
cookies.permanent[:liked] = 'liked1#liked2#'
I removed liked1#:
cookies.permanent[:liked].slice! `liked1#`
I get cookies.permanent[:liked]:
'liked2#'
Next, I removed 'liked2#':
cookies.permanent[:liked].slice! `liked2#`
and I thought I would get '', but I got:
'liked1#'
And I printed cookies.permanent[:liked], I got 'liked1#liked2#'!
I just want to delete a substring of cookies value, but I find it still can be read.
So, how to do that? Note, I must use permanent.
Yeah, I find I can do this by this:
temp = cookies.permanent[:liked]
wanted_deleted = 'liked1#'
temp.slice! wanted_deleted
cookies.permanent[:liked] = temp
Now, value of cookies.permanent[:liked] is 'liked2#'
There has more efficient ways?
Related
I am trying to replace some characters in a text block. All of the replacements are working except the one at the beginning of the string variable.
The text block contains:
[FIRST_NAME] [LAST_NAME], This message is to inform you that...
The variables are defined as:
$fname = "John";
$lname = "Doe";
$messagebody = str_replace('[FIRST_NAME]',$fname,$messagebody);
$messagebody = str_replace('[LAST_NAME]',$lname,$messagebody);
The result I get is:
[FIRST_NAME] Doe, This message is to inform you that...
Regardless of which tag I put first or how the syntax is {TAG} $$TAG or [TAG], the first one never gets replaced.
Can anyone tell me why and how to fix this?
Thanks
Until someone can provide me with an explanation for why this is happening, the workaround is to put a string in front and then remove it afterward:
$messagebody = 'START:'.$messagebody;
do what you need to do
$messagebody = substr($messagebody,6);
I believe it must have something to do with the fact that a string starts at position 0 and that maybe the str_replace function starts to look at position 1.
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
I'm using the gem called "rails3-jquery-autocomplete" ( https://github.com/crowdint/rails3-jquery-autocomplete )
It's working fine:) But there's one thing that I want to disable.
When I type something into an input field, and there's no matching record, it always show the message that's saying "no existing match"
I want to disable this message. Is it possible?
I wish I could see inside of its source code to customize but I cannot :(
Anyone can help me about this?
This might be piece of cake for someone but not for me.
For your information, I've done this command below
$ bundle exec rake assets:precompile RAILS_ENV=production
and after this it created some js files into public/assets folder
Seems there is no way to pass option into the control, but I assume this might be a trick to solve your problem.
Instead of returning empty array(from backend) when no search results, try to return a array with one result has empty id and label might solve your problem, so in your rails controller, there will be something like this:
def autocomplete_action
result = do_the_search
if result.count == 0
result = [{ id: "", label: "" }] # !! do the trick here
end
respond_with result # assume you have "respond_to :json" in your controller
end
Here is why I am doing this:
The reason why I think this might work is because if no result return, the autocomplete control will build a fake record with id equals to empty represent no result.
if(arguments[0].length == 0) {
arguments[0] = []
arguments[0][0] = { id: "", label: "no existing match" } // hacked here
}
But I do think this definitely not the right way to solve this problem, but for now it's workaround to solve your problem quickly.
Hope it helps, thanks.
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!
I wanted to get an object on production and do an exact replica( copy over its contents) to another object of same type. I tried doing this in 3 ways from ruby console which none of them worked:
Let's say you have the tt as the first object you want to copy over and tt2 as the replica object. The first approach I tried is cloning the array
tt2.patients = tt.urls.patients
tt2.doctors = tt.segments.doctors
tt2.hospitals = tt.pixels.hospitals
Second approach I tried is duplicating the array which is actually the same as cloning the array:
tt2.patients = tt.patients.dup
tt2.doctors = tt.doctors.dup
tt2.hospitals = tt.hospitals.dup
Third approach I tried is marhsalling.
tt2.patients = Marshal.load(Marshal.dump(tt.patients))
tt2.doctors = Marshal.load(Marshal.dump(tt.doctors))
tt2.hospitals = Marshal.load(Marshal.dump(tt.hospitals))
None of the above works for deep copying from one array to another. After trying out each approach individually above, all the contents of the first object (tt) are nullified (patients, doctors and hospitals are gone). Do you have any other ideas on copying the contents of one object to another? Thanks.
Easy!
#new_tt = tt2.clone
#new_tt.patients = tt2.patients.dup
#new_tt.doctors = tt2.doctors.dup
#new_tt.hospitals = tt2.hospitals.dup
#new_tt.save
This is what ActiveRecord::Base#clone method is for:
#bar = #foo.clone
#bar.save
Ruby Facets is a set of useful extensions to Ruby and has a deep_clone method that might work for you.