Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

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

Course Queries Syllabus Queries

Max. 2000 characters
Replies

usr_profile.png

User

( 4 months 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

 <%= 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)" %>
usr_profile.png

User

( 4 months ago )

 

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.

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif