使用帮助器方法将html插入到文档中

时间:2014-06-18 19:50:38

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

使用twitter bootstrap作为我们的rails 4应用程序,我想创建一个帮助函数来添加一个链接到页面,当单击时,将生成一个模式弹出窗口,呈现显示模板。我认为我很接近,当从加载调用此函数的页面时,按钮被正确显示,但是当单击时,它会生成一个带有原始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

1 个答案:

答案 0 :(得分:1)

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

应用程序/视图/应用/ _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>

和in application_helper.rb:

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