简体   繁体   English

Rails使用模态和局部变量进行部分渲染

[英]Rails render partial with modal and local variables

I'm trying to render a partial in a popup (ideally a modal that I have to figure out) upon clicking on a "group name" that's not based on any database object, but rather based on local variables derived from an API. 我试图在点击不是基于任何数据库对象的“组名”,而是基于从API派生的局部变量时,在弹出窗口(理想情况下是我必须弄清楚的模态)中呈现部分。 I've seen this older post: Rendering partial in rails modal , but it's simply not working for me. 我见过这个较老的帖子: 在rails模式中渲染部分 ,但它根本不适合我。 When I click, I merely am routed to root. 当我点击时,我只是被路由到root。 I'm currently trying to do this by using display:none on a "render partial" and a .show() function when clicking on the "group name". 我正在尝试通过在“render partial”上使用display:none和在单击“group name”时使用.show()函数来执行此操作。

I'm newish to Rails and really struggling to learn Javascript, so I don't know what's going wrong. 我对Rails很新,真的很难学习Javascript,所以我不知道出了什么问题。

Here's the view: 这是观点:

<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: 使用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();
}

EDITS: FINAL CODE THAT WORKED 编辑:工作的最终代码

$('.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;
});

Two errors in your JS code related to this behavior: JS代码中与此行为相关的两个错误:

  1. click(function() should have an argument e click(function()应该有一个参数e
  2. e.preventDefault() should be put at the first. e.preventDefault()应该放在第一个。 For the last position, the equivalent is return false 对于最后一个位置,等价物return false
  3. [optional] better to use on instead of click [可选]更好地使用on ,而不是click

So, 所以,

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

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

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