【问题标题】:Using a helper method to insert html into document使用辅助方法将 html 插入到文档中
【发布时间】:2014-06-18 19:50:38
【问题描述】:

为我们的 rails 4 应用程序使用 twitter 引导程序,我想创建一个辅助函数来添加一个指向页面的链接,当单击该链接时,将生成一个呈现显示模板的模式弹出窗口。我想我已经接近了,当从加载调用此函数的页面时,按钮会正确显示,但是当单击时,它会产生一个带有原始 ruby​​ 注入的模式,如下所示:

<%= render :template => 'imprintables/show', locals: { modal: true, id: 1 } %>

我的问题是,我希望执行红宝石注射,而不是显示注射。也将不胜感激一般风格的建议和其他指针/批评。谢谢!

application_helper.rb:

module ApplicationHelper
  def imprintable_modal(imprintable) raw %(
    <div class="col-lg-3 text-right">
      <a href="#" class="btn btn-primary" data-toggle="modal" data-target="#basicModal">Click for Imprintable Info</a>
    </div>

    <div class="modal fade" id="basicModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-body">
            <%= render :template => 'imprintables/show', locals: { modal: true, id: #{imprintable.id} } %>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div> )
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby twitter-bootstrap modal-dialog


    【解决方案1】:

    如果我理解正确,并且模板 'imprintables/show' 的内容不需要动态加载(通过 ajax),那么我建议使用普通的旧模板。

    app/views/application/_modal.html.erb:

    <div class="col-lg-3 text-right">
          <a href="#" class="btn btn-primary" data-toggle="modal" data-target="#basicModal">Click for Imprintable Info</a>
        </div>
    
        <div class="modal fade" id="basicModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-body">
                <%= render :template => 'imprintables/show', locals: { modal: true, id: #{imprintable.id} } %>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              </div>
            </div>
          </div>
        </div>
    

    以及在 application_helper.rb 中:

    module ApplicationHelper
      def imprintable_modal(imprintable)
        render :partial => 'modal', :locals => { :imprintable => imprintable }
      end
    end
    

    【讨论】:

    • 我正要发帖说我已经想通了,但你打败了我!我想当这个答案如此明显时,我一直在以一种方式思考。感谢您的回复!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 2022-08-02
    • 2021-02-20
    • 2018-01-02
    • 1970-01-01
    相关资源
    最近更新 更多