如何将模态放入局部?

时间:2012-05-29 13:56:55

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

我无法理解如何将模态放入partials中。请帮帮我!

我有按钮:

 <a class="btn btn-large" data-toggle="modal" href="#show_me_modal"
 onclick="printpage()">Name of button<sup>TM</sup> message in action</a>

和div modal:

    <div class="modal hide fade" id="show_me_modal">
     <div class="modal-header">
      <a class="close" data-dismiss="modal">×</a>
         </div>
      <div class="modal-body">
      <p>some text</p>

      <ul class="media-grid">
        <%= image_tag("/images/pic02.png") %>
      </ul>

    </div>

    <div class="modal-footer">
      <b><%= link_to "#", '#' %></b>          
    </div>
  </div>

1 个答案:

答案 0 :(得分:2)

我为一个项目创建了ModalHelper。它有助于动态创建模态和链接。希望它可以帮到你:

帮助代码:

创建文件app / helpers / modal_helper.rb

module ModalHelper
    def modal(css_id, header_text, hidden = true, &block)
        content_tag(:div, :class => 'modal', :id => css_id, :style => ("display:none;" if hidden) ) do
            concat modal_header(header_text)
            concat modal_body(&block)
        end
    end

    def modal_button(link_text, href)
        modal_caller link_text, href, :button
    end

    def modal_link(link_text, href)
        modal_caller link_text, href
    end

    private

    def modal_caller(link_text, href, type = nil)
        options = { :"data-toggle" => "modal" }
        options.merge!({ :class => "btn" }) if type == :button
        link_to link_text, "#" + href, options
    end

    def modal_header(header_text)
        content_tag(:div, :class => 'modal-header') do
            concat content_tag(:button, 'x', :class => 'close', :"data-dismiss" => 'modal')
            concat content_tag(:h3, header_text)
        end
    end

    def modal_body
        content_tag(:div, :class => 'modal-body') do
            yield
        end     
    end 
end

链接到模态:

生成指向模态的链接。

<%= modal_link('Sign up', "myModal") %>

渲染到模态:

包含您要渲染的代码。

<%= modal('myModal', 'Registration') do %>
    <% render 'devise/registrations/register' %>
<% end %>