Denys Medynskyi
Denys Medynskyi

Reputation: 2353

How to put modals in partials?

I can't get how to put modals in partials. Help me, please !

I have button:

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

and 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>

Upvotes: 0

Views: 765

Answers (1)

thesis
thesis

Reputation: 2575

I have created ModalHelper for one project. It helps dynamically create modals and links to them. Hope it helps you:

Helper code:

Create file 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

Link to modal:

Generates link to your modal.

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

Rendering to modal:

Contains code you want to render.

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

Upvotes: 2

Related Questions