简体   繁体   English

从控制器传递参数到JS.ERB文件? [路轨3]

[英]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 当我“导入”任务时,我希望javascript
1. click "add missions" link (which adds a nested form) 1.点击“添加任务”链接(添加一个嵌套表格)
2. input values of the "imported missions" into the ckeditor textbox. 2.将“导入的任务”的值输入到ckeditor文本框中。

_IMPORT_FORM.HTML.ERB _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 然后转到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. 然后我要呈现IMPORT.JS.ERB文件,并传递@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? 将Ruby参数传递到此Javascript.erb文件中的正确语法是什么? 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 , 好的,IMPORT.JS.ERB是将发送回浏览器的javascript,但是由于扩展名为.ERB,因此在发送给浏览器之前,它将首先在控制器的上下文中由嵌入式ruby处理,因此您可以使用标准ERB语法,

 <%= @something %>  

anywhere in your javascript to manipulate the javascript being sent back, so you could do this: 您可以在JavaScript中的任何位置操作发送回的javascript,因此您可以执行以下操作:

import.js.erb: 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. 如果您的页面上有一个id为“ someday”的div,则应将其内容设置为missions_hash的转储。 This also assumes your are using AJAX, ie: 这也假设您正在使用AJAX,即:

<%= 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: 但是,您需要告诉Rails来使用嵌入式ruby对javascript文件进行预处理,因此您必须进行以下更改:

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. 有一些更简洁的方法可以完成您想要的总体工作,但是我想您现在只需要弄清楚我们嵌入的ruby和javascript组合。

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 如果要将变量传递给js,可以尝试使用gem 客户变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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