javascript - Passing in Params to JS.ERB file from controller? [Rails 3] -
i trying allow users "import" saved missions syllabus post. syllabus has many missions, , syllabus form nested form user can 'add missions,' append new missions textbox.
when "import" missions, want javascript to
1. click "add missions" link (which adds nested form)
2. input values of "imported missions" ckeditor textbox.
_import_form.html.erb
<%= form_tag(import_missions_path, :method => :post, :id => "import-js" ) |f| %> <ul> <% current_user.folders.find_by_order(1).missions.each |mission| %> <li> <%= check_box_tag "mission_ids[]", mission.id %> <%= mission.id.to_s + ". " + mission.title %> </li> <% end %> </ul> <%= submit_tag "import ", :class => 'btn btn-primary' %> <% end %> this goes syllabuses#import
def import @missions_hash = [] #loop through each mission id :missions_id params[:mission_ids].each |id| @missions_hash << mission.find(id) end respond_to |format| format.html { redirect_to edit_syllabus_path(@syllabus), notice: "imported" } format.js { render 'folders/import.js' } end end which want render import.js.erb file, , pass in @missions_hash. code below wrong, , need fixing it.
import.js.erb
//loop each mission passed in <% @mission_hash.each |mission| %> //click add missions $('#add-missions-button').trigger('click'); //pass in mission.title & mission.content form textbox value <% end %> what correct syntax pass in ruby params javascript.erb file? also, want copy 'title' , 'content' of imported missions newly added mission form box:
<%= f.text_field :title, :class =>'row span6' %> <%= f.cktext_area :content, :toolbar => 'mytoolbar', :class => 'row span6', :rows => '5', :placeholder => "what first step learner should do? (e.g. watch intro video, read article)" %> how copy values of these textboxes, user can edit after importing?
i realize question badly organized, tried make simple possible without explaining entire background. i'm still beginner please understand..
ok, import.js.erb javascript sent browser, because of .erb extension, processed embedded ruby first, in context of controller, before being sent browser, can use standard erb syntax ,
<%= @something %> anywhere in javascript manipulate javascript being sent back, this:
import.js.erb:
$('#somediv').html('<%= escape_javascript(@missions_hash.inspect) %>'); if have div on page id = 'someday', it's contents should set dump of missions_hash. assumes using ajax, i.e.:
<%= form_tag(import_missions_path, :method => :post, :id => "import-js" , :remote=>true) |f| %> but, need tell rails pre-process javascript file embedded ruby, you'd have change:
render 'folders/import.js' to
render 'folders/import.js.erb' there cleaner ways accomplish want overall, think have figure our embedded ruby , javascript combination.
Comments
Post a Comment