I have an array that I am passing to a javascript file.
In the controller I have the array printing out and it appears like so...
["123456789", "987654321", "147852369", "369852147"]
This array is set to the instance variable @array in the controller.
I'm sending this array to a JavaScript file by setting @array
as a variable in the view.
var array = "<%= @array %>";
Note: I've tried var array = "<%=raw @array.to_json %>";
as well.
Then in the JavaScript file I am simply printing the array in the console like so...
console.log(array);
However when it is printed in the console the output looks like this.
["123456789", "987654321", "147852369", "369852147"]
When I check the typeof through jQuery it es back as a string instead of an array.
I do not want to see the "
. Eventually I want to use these values as selectors when running some JavaScript functions.
Through these steps, what am I doing wrong that is producing the "
?
edit: This is how I am initially creating my array.
CONTROLLER
@original= large_array_of_hashes
# I am grabbing only specific data from the hash. Only what es after @errors=>
@data = @original.scan(/@errors=>"(.*?)"/).flatten.map{ |msg| msg.gsub(/(\.|\s+)/, '').strip }
@array = []
@data.each{|data| @array << data}
I have an array that I am passing to a javascript file.
In the controller I have the array printing out and it appears like so...
["123456789", "987654321", "147852369", "369852147"]
This array is set to the instance variable @array in the controller.
I'm sending this array to a JavaScript file by setting @array
as a variable in the view.
var array = "<%= @array %>";
Note: I've tried var array = "<%=raw @array.to_json %>";
as well.
Then in the JavaScript file I am simply printing the array in the console like so...
console.log(array);
However when it is printed in the console the output looks like this.
["123456789", "987654321", "147852369", "369852147"]
When I check the typeof through jQuery it es back as a string instead of an array.
I do not want to see the "
. Eventually I want to use these values as selectors when running some JavaScript functions.
Through these steps, what am I doing wrong that is producing the "
?
edit: This is how I am initially creating my array.
CONTROLLER
@original= large_array_of_hashes
# I am grabbing only specific data from the hash. Only what es after @errors=>
@data = @original.scan(/@errors=>"(.*?)"/).flatten.map{ |msg| msg.gsub(/(\.|\s+)/, '').strip }
@array = []
@data.each{|data| @array << data}
Share
Improve this question
edited Mar 19, 2015 at 19:36
mikeymurph77
asked Mar 19, 2015 at 19:09
mikeymurph77mikeymurph77
8021 gold badge12 silver badges28 bronze badges
4
-
Try
<%=raw @array.to_json.html_safe %>
– Sonalkumar sute Commented Mar 19, 2015 at 19:20 -
Unfortunately that did not work... I got an
SyntaxError: Unexpected number
error – mikeymurph77 Commented Mar 19, 2015 at 19:23 -
TRy this
<%= @array.to_json.flatten.to_json.html_safe %>
or just<%= raw @array %>
– Sonalkumar sute Commented Mar 19, 2015 at 19:24 -
The first option gave me an
internal server error
and the second method gave me the sameSyntaxError: Unexpected number
.... I've updated my question to include how I am creating my array. – mikeymurph77 Commented Mar 19, 2015 at 19:37
3 Answers
Reset to default 4My issue was corrected by just prepending raw to my ruby variable
<%= raw @array %>
I know it's late but hope it helps someone down the road!
TRy this, place them in single quotes
IN javascript
var array = '<%= @array.to_json %>';
array.replace(/"/g, '"');
I think that
@array = %w[123456789 987654321 147852369 369852147]
and then your javascript things will work.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745313553a4622111.html
评论列表(0条)