简体   繁体   中英

Passing in Params to JS.ERB file from controller? [Rails 3]

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

 <%= form_tag(import_missions_path, :method => :post, :id => "import-js" )  do |f| %>
  <ul>
  <% current_user.folders.find_by_order(1).missions.each do |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 then goes to SYLLABUSES#IMPORT

def import
@missions_hash = []
    #loop through each mission id from :missions_id
params[:mission_ids].each do |id|
 @missions_hash << Mission.find(id)
end
respond_to do |format|
  format.html { redirect_to edit_syllabus_path(@syllabus), notice: "imported" }
  format.js { render 'folders/import.js' }
end
end

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

//loop for each mission passed in 
<% @mission_hash.each do |mission| %>
  //click add missions
  $('#add-missions-button').trigger('click');
  //pass in mission.title & mission.content to form textbox value
<% end %>

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:

  <%= f.text_field :title, :class =>'row span6' %>
  <%= f.cktext_area :content, :toolbar => 'MyToolbar', :class => 'row span6', :rows => '5', :placeholder => "What is the first step a learner should do? (e.g. Watch an intro video, read certain article)" %>

How would I copy it into the values of these textboxes, so the user can edit it after importing?

I realize this question is badly organized, but I tried to make it as simple as possible without explaining the entire background. I'm still a beginner so please understand..

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, ie:

<%= 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.

i think you shouldn't do that, because you will meet problem when sprocket compile. It will compile in the first time when file change. if you want to pass variable to js you can try that gem client variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM