I think this is easily explained by looking at code, so I’m posting a couple of snippets that show how I usually do Ajax things in Rails 3.2,
The following will work:
create.js.coffee
$("<%= raw j(render(partial: @comment, locals: { str: 'string' })) %>")
.appendTo("ul#comments")
_comment.html.haml
-puts "The variable's value is #{str}"
%li= comment.what
Now, I’d like to do the same as above, but instead of passing a JS string literal, I’d like to pass an object literal, like this:
create.js.coffee
obj =
'v1': 'value'
'v2': 'value'
$("<%= raw j(render(partial: @comment, locals: { o: obj })) %>")
.appendTo("ul#comments")
Which of course fails with the following reason:
ActionView::Template::Error (undefined local variable or method `obj’…
Does anybody know how to accomplish what I’m trying to do? Is it possible at all?
Let me know if what I’m trying to do is insane, and what would be the proper way if that’s the case..
Thanks!
Well I guess clients objects are not shared with server object, unless untill we are doing remoting. So in this case you have to serialize JS object into JSON or any other string format (XML or any other structure) and then you have to de-serialize in Ruby object or you can manually parse the string and use the information provided.