简体   繁体   中英

Rails link_to in bootstrap modals

I have do each loop for a bunch of items that users can vote on. The items are listed on the index page, with each item as a button that opens up a Bootstrap modal on click. I want the user to be able to vote on the item within the modal, however I'm having difficulty accomplishing this. Here's what I have:

$(function () {
   $(".box-inner").click(function() {
   $('#modal_title').empty(); 
   id = $(this).data('id');
   url = window.location.origin + '/my_items/' + id + '/get_modal_content';

   $.ajax({
    cache: false,
    type: 'GET',
    url: url,
    data: id,
    complete: function() {
      $.getJSON(url, function (data) {
        $('#modal_title').append(data.name);
        $('#myModal').attr("data-id", id);
      });
     },
     success: function(data) 
     {
      $("#myModal").modal('show');
     }
    });
  });
});

Here is what the modal looks like:

<div id="myModal" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
        <h3 id="modal_title"></h3>
      </div>
      <div class="modal-body">
        <div class="technique-info-section">
         <h3>Info</h3>
         <span class="thumbs-up">
          <%= link_to like_my_item_path(**???**), method: :put, :remote => true do %>
            <i class="fa fa-thumbs-o-up"></i>
          <% end %>
         </span>
         <span class="thumbs-down">
           <%= link_to dislike_my_item_path(**???**), method: :put, :remote => true do %>
             <i class="fa fa-thumbs-o-down"></i>
            <% end %> 
         </span>
        </div>
      </div>
    </div>
   </div>
 </div>

And here is what the .each loop looks like:

<% @techniques.each do |technique| %>
  <li class="technique-box-outer">
    <button class="box-inner" data-id="<%= technique.id %>">
      <p class="technique-name"><%= technique.name %></p>
    </button>
  </li>
<% end %>

So in the two link_to paths in the modal, how can I get the link_to helper methods to have the correct item from the @techniques .each loop so that I can pass those params to my controller?

Just use it when you have it, and retain it for later, like so:

<%= button_tag data: {id: technique.id, like_path: like_my_item_path(technique), dislike_path: dislike_my_item_path(technique) } do %>
  <%= content_tag :p, technique.name, class: 'technique-name' %>
<% end %>

Then, in your js, you can do stuff like:

like_path = $(this).data('like-path');
dislike_path = $(this).data('dislike-path');

$("#myModal .thumbs-up a").attr('href', like_path)
$("#myModal .thumbs-down a").attr('href', dislike_path)

As a side-note, you may wish to bind the click to a higher-level element, to avoid issues with dynamic replacement ( like turbolinks, for example ). Something like this:

$(document).on 'click', '.box-inner', function() {}

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