简体   繁体   中英

Pass Rails variable using link_to modal which is used in a helper

I'm having trouble understanding how to pass in a variable to modal, so that I can use (not as an input in a form) but to use in a helper method.

I've looked at: Passing data to a bootstrap modal and How to pass values arguments to modal.show() function in Bootstrap and Bootstrap JavaScript

Link_to modal:

<%= link_to "#{comment.fname}", "#commenterModal", :class => "btn commenter", "data-toggle" => "modal", "data-id" => "4"%>

I'm using data-id="4" to test, but I would be passing in Rails variable comment.id.

Modal:

<div id="commenterModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-body" style="background-color: #F5F5F5;">
        <div class="row" id="commentId">


         <%= getuserprofileofcommenter(commentId).bio %> 
        </div>

      <div class="modal-footer">
        <button class="btn btn-primary" data-dismiss="modal" aria-hidden="true">OK</button>
      </div>

      </div>
    </div>
  </div>
</div>

JS:

$(document).ready(function() {
    $('#commenterModal').on('show.bs.modal', function(event) {
        $("#commentId").val($(event.relatedTarget).data('id'));
    });
});

I know I'm not understanding this correctly. But I'm trying to take this instance when I click on the link_to, pass the variable (comment.id) in to the modal so I can use it when I call the helper method "getuserprofileofcommenter(commentId)".

Any insight would help. Thank you!

As I understand, you want to change content of a modal each time you click on the comment link.

I usually take help of rails ajax in such scenario.

Extract modal content to a partial _show_modal.html.erb

<div id="commenterModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-body" id="commentUserModal">
         <%= getuserprofileofcommenter(@comment.id).bio if @comment.present? %> 
      </div>
      <div class="modal-footer">
        <button class="btn btn-primary" data-dismiss="modal" aria-hidden="true">OK</button>
      </div>
    </div> <!-- modal-content -->
   </div>  <!-- modal-dialog -->
</div>     <!-- modal -->

Template containing link_to :

<%= link_to comment.fname, show_modal_groups_path(comment_id: comment.id), :class => "btn", remote: true %>

<!-- rendering partial -->
<div id="commentUserModal">
 <%= render 'show_modal' %>
</div>

comments_controller.rb

def show_modal
  @comment = Comment.find_by_id(params[:comment_id])
  respond_to do |format|
     format.js {render 'show_modal'}
  end
end

comments/show_modal.js.erb

$("#commentUserModal").html("<%= j render 'show_modal' %>");
$("#commenterModal").modal('show');

This code is not tested.

Hope it helps.

This is not possible as you described it. The JS is running after the server responds so any rails helpers can not be invoked during this time. (rails helpers only run on the server side when the view(s) is being rendered)

The way to accomplish what you want is to do an ajax request to the server when a link is clicked and in your js response, you will have neccessary state (provided by the controller) to interact with the already open modal

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