【问题标题】:Rails 3. Not able to pass locals in a Partial js file with localsRails 3. 无法在局部 js 文件中通过局部变量传递局部变量
【发布时间】:2013-07-28 10:40:41
【问题描述】:

我有很多 js 文件,如 new.js、index.js、create.js 等,用于处理我的 Ajax 调用和其他 jquery 代码 由于有许多通用代码 sn-ps 我尝试使用部分 js 文件 例如,我有一个调用部分 _new.js 的 new.js 文件

new.js

    <%= render :partial=>'new',  :formats=>[:js],:handlers=>[:erb] %>

_new.js

    <% if params[:group_id]%>
      $("#group_participants_content").html("<%= escape_javascript(render(:partial=>'form'))%>");
      $(".remote_selected").removeClass("selected remote_selected simple-navigation-active-leaf");
      $("#group_mail").addClass(" remote_selected simple-navigation-active-leaf");
   <%else%>
     $("#users_content").html("<%= escape_javascript(render(:partial=>'form'))%>");
     $(".remote_selected").removeClass("selected remote_selected simple-navigation-active-leaf")
     $("#mail").addClass("remote_selected simple-navigation-active-leaf");
  <%end%>
  // Change our States
History.replaceState({action:'<%= "#{controller.action_name}/#{controller_name}" %>'}, '<%=       "#{controller.action_name.titleize} #{controller_name}" %>', '?action=<%=  "#{controller.action_name}/#{controller_name}" %>');
var url=History.getState().url;   
$("#help").attr('href',"/helps/0?url="+url)

这可行,但为了有用,我想将本地人添加为

    <%= render :partial=>'new',  :formats=>[:js],:handlers=>[:erb], :locals=>  {:id=>"#group_participants_content"}%>

并将 _new.js 更改为

  $('"'+id+'"').html("<%= escape_javascript(render(:partial=>'form'))%>");

但这不起作用-js文件似乎根本没有加载

任何人有什么错误的建议?

感谢任何帮助?

【问题讨论】:

    标签: ruby-on-rails jquery partial locals


    【解决方案1】:

    这是

    $("<%=id%>").html("<%= escape_javascript(render(:partial=>'form'))%>");
    

    【讨论】:

    • 不用担心。乐于助人。顺便说一句,如果是的话,你可以接受它作为答案。
    【解决方案2】:

    来的id已经在引号中了,你的代码会再次用引号括起来,直接传递。希望我能工作:

    $(id).html("<%= escape_javascript(render(:partial=>'form'))%>");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-21
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 2012-12-07
      • 2016-08-23
      • 2023-03-14
      相关资源
      最近更新 更多