Reputation: 12072
Apologies for the "not so great questions". This happens when your eyes are closed but your brain keeps thinking about code.
This bit puzzles me. I want to split a string on a comma to make it an array, OR..., if I can send to my controller the array then perfect but anything I do never seem to work so what may work is to split on the second comma in a string.
I have a state array that looks like this:
So when I use params[:file].split(",")
I get a handle error for data:image/jpeg;base64
because it splits on the first comma when the code itself it correct. A stupid question, can you split on the second comma in ruby?
The array is like: ["data:image/jpeg;base64,/9j/xxxxxx,data:image/jpeg;base64,/9j/xxxxxx"]
My input looks like this and I have tried many variations with file[]
, file[][]
<input type="hidden" name="file" value={this.state.files} />
I've used concat
to get the array.
Upvotes: 0
Views: 930
Reputation: 110675
str = "Now is the time for citizens, every last one, to revolt, without delay."
i1 = str.index(',')
i2 = i1 + 1 + str[i1+1..-1].index(',')
[str[0..i2-1], str[i2+1..-1]]
# => ["Now is the time for citizens, every last one",
# "to revolt, without delay."]
Upvotes: 0
Reputation: 736
result = []
incoming_data.gsub(/"data:\S+"/) {|member| result << member}
maybe this way...
Upvotes: 0
Reputation: 4570
You could also serialize the files array in a different manner before sending to Rails.
What does the code that sends files look like?
If anything, can you join it like this, with some unique separator?
makeRequest({ file: this.state.files.join('---') });
And then split with that in Ruby
params[:file].split('---')
Upvotes: 3
Reputation: 51
params[:files].split(",").each_slice(2).map { |top| "#{top.first},#{top.last}"}
Basically you can split by "," as normal but then merge every two elements together : )
str = '["data:image/jpeg;base64,/9j/xxxxxx,data:image/jpeg;base64,/9j/xxxxxx"]'
str.split(",").each_slice(2).map{ |top| "#{top.first},#{top.last}"}
=> ["[\"data:image/jpeg;base64,/9j/xxxxxx", "data:image/jpeg;base64,/9j/xxxxxx\"]"]
Upvotes: 1