轨道使用模态变量和局部变量呈现部分

Rails render partial with modal and local variables

本文关键字:局部变量 现部 变量 模态 轨道      更新时间:2023-09-26

我试图在弹出窗口中呈现一个部分(理想情况下是我必须弄清楚的模式),单击一个不基于任何数据库对象的"组名",而是基于从 API 派生的局部变量。我看过这篇较旧的帖子:在轨道模式中渲染部分,但它根本不适合我。当我点击时,我只是被路由到根。我目前正在尝试通过在单击"组名称"时使用"渲染部分"和 .show() 函数上的 display:none 来做到这一点。

我对Rails很陌生,并且真的很难学习Javascript,所以我不知道出了什么问题。

这是视图:

<div id="groups_show" style="display:none">
  <%= render partial: 'groups/group_full', :locals => {group: group} %>
</div>
<%= link_to group.name, root_url, class: "groups_showme" %>

Javascript:

$('.groups_showme').click(function() {
  $('#groups_show').show();
  win = new Window({title: "Share This", width:200, height:150, destroyOnClose: true,     
  recenterAuto:false});
  win.setContent('#groups_show',true,true);
  win.show();
}

编辑:有效的最终代码

$('.groups_showme').on('click', function(e) {
 e.preventDefault();
 $('#groups_show').show();
 win = new Window({title: "Share This", width:200, height:150, destroyOnClose: true, recenterAuto:false});
 win.setContent('#groups_show',true,true);
 win.show();
 return false;
});

JS 代码中与此行为相关的两个错误:

  1. click(function()应该有一个论据e
  2. e.preventDefault()应该放在第一位。对于最后一个位置,等效值为 return false
  3. [可选] 最好使用 on 而不是click

所以

$('.groups_showme').on('click', function(e) {
  e.preventDefault();
  // others
});