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

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer 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

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

0 views
0 shares
profilepic.png
manpreet 2 years 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.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community