Sylar Sylar - 5 months ago 17
Ruby Question

Split on second comma in ruby

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:
enter image description here

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.

Answer

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('---')
Comments