简体   繁体   English

使用在助手中使用的 link_to 模式传递 Rails 变量

[英]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我看过: Passing data to a bootstrap modalHow to pass values arguments to modal.show() function in Bootstrap and Bootstrap JavaScript

Link_to modal: Link_to 模态:

<%= 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.我使用 data-id="4" 进行测试,但我会传入 Rails 变量 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: 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)".但是,当我单击 link_to 时,我正在尝试采用此实例,将变量 (comment.id) 传递到模态,以便在调用辅助方法“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.在这种情况下,我通常会使用 rails ajax 的帮助。

Extract modal content to a partial _show_modal.html.erb将模态内容提取到部分_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模板:

<%= 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评论控制器.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评论/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. JS 在服务器响应后运行,因此在此期间无法调用任何 Rails 助手。 (rails helpers only run on the server side when the view(s) is being rendered) (rails 助手仅在渲染视图时在服务器端运行)

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完成您想要的方法是在单击链接时向服务器发出 ajax 请求,并且在您的 js 响应中,您将拥有必要的状态(由控制器提供)与已经打开的模式进行交互

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

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