Skip to content

Instantly share code, notes, and snippets.

@wojtha
Forked from coreyward/README.md
Created December 5, 2012 18:39
Show Gist options
  • Save wojtha/4218293 to your computer and use it in GitHub Desktop.
Save wojtha/4218293 to your computer and use it in GitHub Desktop.
<div id="modal-container"></div>
<div id="modal">
<a href="#" class="close">close</a>
</div>
// This partial should be @imported into your layout Sass file, or included via the Asset Pipeline
//
#modal-container {
display: none;
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0,0,0,0.4);
}
#modal {
display: none;
position: absolute;
width: 600px;
left: 50%;
margin-left: (-600px - 40px) / 2;
padding: 20px;
background: #fff;
border: 5px solid #eee;
& > .close {
position: absolute;
right: 5px;
top: 5px;
color: #666;
&:hover, &:active {
color: #000;
}
}
}
//= jquery
//= jquery_ujs
//= modals
# in your app controller, you'll want to set the layout to nil for XHR (ajax) requests
class ApplicationController < ActionController::Base
layout Proc.new { |controller| controller.request.xhr? ? nil : 'application' }
end
<%= link_to 'New Post', new_post_path, :remote => true %>
# This file should be "required" into your `application.js` via the Asset Pipeline.
#
$ ->
$modal = $('#modal')
$modal_close = $modal.find('.close')
$modal_container = $('#modal-container')
# Handle modal links with the data-remote attribute
$(document).on 'ajax:success', 'a[data-remote]', (xhr, data, status) ->
$modal
.html(data)
.prepend($modal_close)
.css('top', $(window).scrollTop() + 40)
.show()
$modal_container.show();
$(document).on 'click', '#modal .close', ->
$modal_container.hide()
$modal.hide()
false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment