【问题标题】:Application Wide Modal Issue rails 5 Bootstrap 3应用范围的模态问题 rails 5 Bootstrap 3
【发布时间】:2016-08-29 05:11:53
【问题描述】:

我在用户控制器中为我的新操作和编辑操作设置了模式。为了保持显示的一致性,我希望我的用户显示页面也出现在模式中。

现在用户通过

访问他们的节目页面
<li><%= link_to "View Profile", current_user %></li>

在我的导航栏中链接。

我想设置此链接以在我的用户控制器中显示所需的显示操作模式。

我被卡住了,我试图用谷歌搜索如何做到这一点,但我认为我问的问题不正确。如果有人可以指导我获取资源或给我如何实现这一目标的大致概述,将不胜感激!

我意识到这个问题有点含糊,但我不知道该怎么做。

【问题讨论】:

    标签: javascript jquery css ruby-on-rails twitter-bootstrap


    【解决方案1】:

    如果您只想在单击时加载配置文件,则可以使用 rails js。

    1.在链接标签上设置remote: true

    <li><%= link_to "View Profile", current_user, remote: true %></li>
    

    2. 创建一个包含模态的部分,以便我们可以通过 rails js 渲染它。

    app/views/users/_show.html.erb

    <%= content_tag :div, class: "modal fade", id: dom_id(@user, :modal) do %>
      <div class="modal-dialog">
        <div class="modal-content">
          Lorem ipsum...
        </div>
      </div>
    <% end %>
    

    3. 附加包含模态的部分,然后使用 rails js 切换它。当设置了remote: true 时,Rails 会寻找对应的.js.erb 视图。

    app/views/users/show.js.erb

    $('body').append('<%= j render "users/show" %>');  // Append modal
    $('#<%= dom_id @user, :modal %>').modal('toggle'); // Toggle modal
    

    【讨论】:

    • 我需要在 show 动作中添加一个格式块并添加 format.js 吗?
    • 仅当您同时支持 .html 和 .js @ShawnWilson
    • 开始了,我添加了 :remote => true 而不是 remote:true 再次感谢救助!
    • 干得好,但如果你有当前用户对象,那么为什么我们需要访问控制器操作?
    • 好吧,user_path(@user)current_user 都将路由到相同的操作。只需将remote: true 添加到任何用户链接,此模式就适用于整个应用程序。我假设 hed 会为两者重用这个模式。 @uzaif
    【解决方案2】:

    可能是这样的:

    <!-- Trigger the modal with a button -->
    <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
    
    <!-- Modal -->
    <div id="myModal" class="modal fade" role="dialog">
      <div class="modal-dialog">
    
        <!-- Modal content-->
        <div class="modal-content">
          <%= render 'users/show', user: current_user %>
        </div>
    
      </div>
    </div>
    

    【讨论】:

    • 但是我可以将用户显示控制器操作添加到应用程序控制器吗?还是渲染减轻了这样做的需要?
    猜你喜欢
    • 2019-01-25
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 2023-04-08
    • 2013-09-30
    • 1970-01-01
    相关资源
    最近更新 更多