加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – ROR将params传递给模态

发布时间:2020-12-16 19:09:44 所属栏目:百科 来源:网络整理
导读:我正在尝试打开一个显示项目列表的模态.项目列表在视图中已作为变量提供.如何将此变量传递给模态? 下面是主视图的大致情况: View.html.erb div %= @users.each do |user|% h1user.name/h1 h2user.email/h2 %= link_to remote: true,'data-toggle' = 'modal
我正在尝试打开一个显示项目列表的模态.项目列表在视图中已作为变量提供.如何将此变量传递给模态?

下面是主视图的大致情况:

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,and material-modal. If you are using something different from these ( maybe slim,haml,coffee,bootstrap…),you will have to suite the example to your case.

希望这可以帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读