简体   繁体   English

从 jQuery ruby 在轨道上渲染部分

[英]Render partial from jQuery ruby on rails

I wanted to render a partial that contains a form inside of view from application js, I am reading the event of attached button without submitting because I need to process the headers of a file CSV before doing submit.我想渲染一个包含应用程序 js 视图内部表单的部分,我正在读取附加按钮的事件而不提交,因为我需要在提交之前处理文件 CSV 的标题。

This is the function:这是 function:

$(document).ready(function () {
  $("#attached_attached_csv").change(function (e) {
    if (e.target.files != undefined) {
      var reader = new FileReader();

      reader.onload = function (e) {
          // this line isn't working - I try of many ways, but none are working
          $("#text").html("<%= j render partial: 'layouts/form') %>".html_safe);
      };
      reader.readAsText(e.target.files.item(0));
    }
    return false;
  });
});

I see also how any people using ajax, but I don't know how to use this for the trigger event of an attached button without submitting, I prob sending without html_safe but I obtain the string:我还看到有人如何使用 ajax,但我不知道如何在不提交的情况下将其用于附加按钮的触发事件,我可能在没有 html_safe 的情况下发送但我获得了字符串:

<%= j render partial: 'layouts/form') %>

My partial is "layouts/form", which is a file with extension html.erb.我的部分是“布局/表单”,这是一个扩展名为 html.erb 的文件。 here share where I found the code for handle the event http://jsfiddle.net/FSc8y/2/ , but it's not important the most important for me its render the embedded ruby form.在这里分享我在哪里找到了处理事件http://jsfiddle.net/FSc8y/2/的代码,但对我来说最重要的是它呈现嵌入式 ruby 表单并不重要。 Thanks in advance.提前致谢。

You can use rails syntax in your javascript files by adding .js.erb extension to the file name like my_file.js.erb .您可以在 javascript 文件中使用 rails 语法,方法是在文件名中添加.js.erb扩展名,例如my_file.js.erb

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

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