OK, IMPORT.JS.ERB is javascript that will be sent back to your browser, but because of the .ERB extension, it will be processed by embedded ruby first, in the context of the controller, before being sent to the browser, so you can use standard ERB syntax ,
<%= @something %>
anywhere in your javascript to manipulate the javascript being sent back, so you could do this:
import.js.erb:
$('#somediv').html('<%= escape_javascript(@missions_hash.inspect) %>');
If you have a div on your page with id = 'someday', it's contents should be set to a dump of missions_hash. This also assumes your are using AJAX, i.e.:
<%= form_tag(import_missions_path,
:method => :post,
:id => "import-js" ,
:remote=>true) do |f| %>
BUT, you need to TELL rails to pre-process the javascript file with embedded ruby, so you'd have to change:
render 'folders/import.js'
to
render 'folders/import.js.erb'
There are cleaner ways to accomplish what you want overall, but I think you for now you just have to figure our the embedded ruby and javascript combination.
manpreet
Best Answer
2 years ago
I am trying to allow users to "import" saved Missions into a Syllabus post. A Syllabus has many Missions, and the Syllabus form is a nested form where the user can 'Add Missions,' which will append a new Missions textbox.
When I "import" missions, I want the javascript to
1. click "add missions" link (which adds a nested form)
2. input values of the "imported missions" into the ckeditor textbox.
_IMPORT_FORM.HTML.ERB
this then goes to SYLLABUSES#IMPORT
which I then want to render IMPORT.JS.ERB file, and pass in the @missions_hash. The code below is probably wrong, and this is where I need help fixing it.
IMPORT.JS.ERB
What is the correct syntax to pass in the Ruby params into this Javascript.erb file? Also, I want to copy the 'title' and 'content' of the imported Missions into the newly added mission form box: