我正在尝试打开一个显示项目列表的模态.项目列表在视图中已作为变量提供.如何将此变量传递给模态?
下面是主视图的大致情况:
View.html.erb
<div> <%= @users.each do |user|%> <h1>user.name</h1> <h2>user.email</h2> <%= link_to remote: true,'data-toggle' => 'modal','data-target' => '#taskModal do %> <i><%= user.tasks.count%></i> <% end %> <% end %> </div> <div class="modal" id="taskModal" aria-hidden="true"> ... <%= render partial: 'list_modal' %>
list_modal是具有模态结构的部分.
解决方法
与在视图中已经存在项目列表并且想要将其传递给模态相反,您可以做的一件事是使用按钮(或链接)打开模态以通过ajax调用控制器操作,然后让动作响应JS,您现在将使用相应的view.js文件来填充模式以及以编程方式呈现它(填充后).
例:
链接到开放模式:
<%= link_to "Open users list",users_open_list_modal_path,remote: true %>
用户打开列表模态控制器动作:
def open_list_modal @user_list = User.all respond_to do |format| format.js end end
open_list_modal.js.erb:
#populate modal $('#modal').html('#{ escape_javascript(<%= render partial: 'user_list',locals: {user_list: @user_list} )}'); #render modal $('#modal').openModal();
然后在模态本身上,您现在可以访问user_list(NOT @user_list)列表.
Note: In this example,I made use of both
erb
,js
,andmaterial-modal
. If you are using something different from these ( maybeslim
,haml
,coffee
,bootstrap
…),you will have to suite the example to your case.
希望这可以帮助.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。