I am using Rails 3.0.5
I have a javascript code that uses jQuery dropdown checklist and I would like to have the checkboxes displaying which item is checked already and which is not, when using my form in the ‘Edit’ action of my belongings controller.
Basically, it can work like this in Javascript:
$('element').val([1,3,4]);
$('element').dropdownchecklist();
Let us say that in my controller, edit action, I have a instance variable called @selected_items that contains the items that are selected (here: @selected_items = [1,3,4]) and I would like to use the value of this instance variables into my Javascript code, how could I proceed ? How could I access the value of this instance variable in a .js file ?
Many thanks for your answer !
Just write the javascript in your view, and at the appropriate place, print the
@selected_itemsarray as json:app/views/*/*.html.erbYou can also use the Gon gem if
to_jsoncannot serialize more complicated objects.