使用帮助程序中使用的模态传递轨道变量link_to
Pass Rails variable using link_to modal which is used in a helper
我很难理解如何将变量传递给模态,以便我可以使用(不是作为表单中的输入),而是在辅助方法中使用。
我看过:将数据传递到引导模式和如何将值参数传递给 Bootstrap 和 Bootstrap JavaScript 中的 modal.show() 函数
Link_to模式:
<%= link_to "#{comment.fname}", "#commenterModal", :class => "btn commenter", "data-toggle" => "modal", "data-id" => "4"%>
我正在使用 data-id="4" 进行测试,但我会在 Rails 变量 comment.id 传递。
模 态:
<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'));
});
});
我知道我没有正确理解这一点。但是当我单击link_to时,我正在尝试使用此实例,将变量 (comment.id) 传递给模态,以便在调用辅助方法"getuserprofileofcommenter(commentId)"时可以使用它。
任何见解都会有所帮助。谢谢!
据我了解,您希望每次单击评论链接时都更改模态的内容。
在这种情况下,我通常会帮助 rails ajax。
将模态内容提取到部分_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 -->
模板包含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
评论/show_modal.js.erb
$("#commentUserModal").html("<%= j render 'show_modal' %>");
$("#commenterModal").modal('show');
未测试此代码。
希望对您有所帮助。
这不可能像你描述的那样。 JS在服务器响应后运行,因此在此期间无法调用任何rails帮助程序。 (Rails 帮助程序仅在渲染视图时在服务器端运行)
完成您想要的方法是在单击链接时向服务器执行 ajax 请求,并且在您的 js 响应中,您将拥有与已经打开的模式交互的必要状态(由控制器提供)
相关文章:
- 将函数的上下文应用于javascript变量
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- 将PHP变量传递给jQuery时遇到问题
- 如何通过ajax刷新JSF填充的javascript变量
- 参数变量出现ngTable指令问题
- 通过javascript重定向html传递php变量
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 全局变量和全局对象的属性之间有什么区别吗
- 如何在Bootstrap Modal中为动态点击生成的变量设置jade属性
- 值对象在某个变量发生更改后发生更改
- Javascript将变量检测为Hyperlink,并将其显示为Link
- 如何在link中访问angular指令隔离的作用域变量?
- 如何将URL编码解码为$link = $_GET[' URL '];变量
- 在{}和link中为angular指令定义独立作用域变量的区别
- 将javascript变量传递给gsp g:link标签
- 在带有指令的link方法中使用独立的作用域变量
- 如何在Angular Js中访问指令的link部分中的变量?
- 不变量冲突:使用Link &表
- 在我的JavaScript文件中添加变量到href link