UPDATE: here is Rails 5 Adapting this answer using CoffeeScript: https://gist.github.com/1456815
jQuery is more popular for good reasons, which I will not enter. Much about this elsewhere.
Assuming you want to use jQuery, the setup is pretty simple:
And then update your Application.rb JavaScript Application Extension Extensions:
And make sure your layout file (e.g. application.html.erb) contains these files:
<%= javascript_include_tag :defaults %>
For your modal, there are many opinions on how to do this. Personally, I prefer to minimize my own modal windows based on the needs of the application. These are just a few jQuery-laced JS lines for creating modals that work fine with Rails.js:
# in a view, perhaps a _modal.html.erb partial that is included into # your layout (probably at the bottom). <div id="modal-container"></div> <div id="modal"> <a href="#" class="close">close</a> </div>
Here are some styles (scss style) for the modal that I use:
#modal-container { display: none; position: fixed; top: 0; right: 0; bottom: 0; left: 0; background: rgba(0,0,0,0.4); }
This eliminates styles / looks from the path. You might want to customize these styles to fit your application, but they are pretty general, so they should get you started.
To integrate it into Rails, there are 2 parts. First, you need the browser to send AJAX requests for content and show a modal return. Secondly, you need Rails for the answer with the corresponding answer.
Submitting AJAX and processing the responses entails the use of :remote => true in the links / forms that you want to send remotely.
<%= link_to 'New Post', new_post_path, :remote => true %>
This will create a data attribute by the link that Rails.js will receive, allowing it to be sent remotely automatically. It expects javascript to return, and it will execute this response when it receives it. You can add a quick format.js to each respond_to action respond_to and create an accompanying new.js.erb file containing the JS needed to actually populate and display the modality. This is good, but we can DRY this a little more than that by returning a template without a layout and moving the modal show / hide responsibilities in application.js:
javascript to make this all work:
# in application.js $(function(){ var $modal = $('#modal'), $modal_close = $modal.find('.close'), $modal_container = $('#modal-container');
I have some logic to put a 40px modal window from the current scroll position of the browser.
When using all of these elements in place, when you click on the link / form using a set of deleted attributes, Rails.js will process the request sending, your application will know that it returns only a template for an action without a layout, ideal for displaying it in a modal window and then our javascript above will connect to the ajax: success callback from Rails.js and display the modal HTML template returned from our application.
There are more than one way cats cats and more than a dozen ways to create this functionality. This is definitely an area that the Rails team has not yet established stronger conventions. This is just my way of dealing with this, that I feel pretty DRY, requires minimal effort and quite flexible and reliable in the future. Perhaps one of the areas that someone could probably build on is to use a modular / lightbox framework that spans more bases and has more functionality. And if someone does, I would like to write a letter!