简体   繁体   中英

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. I've seen this older post: Rendering partial in rails modal , but it's simply not working for me. When I click, I merely am routed to 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".

I'm newish to Rails and really struggling to learn Javascript, so I don't know what's going wrong.

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:

$('.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:

  1. click(function() should have an argument e
  2. e.preventDefault() should be put at the first. For the last position, the equivalent is return false
  3. [optional] better to use on instead of click

So,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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