Adam Templeton
Adam Templeton

Reputation: 4617

Calling a JavaScript function from a Rails Controller

The project I'm working on renders several pages (such as login pages and signup forms) as modals, which is accomplished by giving those links the class .modal so the following piece of JavaScript would trigger:

$('.modal').click(function(){
    var url = this.href;
    var dialog = $('<div id="modal" style="display:none"></div>').prepend('#barhappy_container');
    dialog.load(url, function(){
        dialog.dialog({
            modal: true,
            draggable: false,
            position: 'center',
            dialogClass: 'no-title',
            width: 'auto',
/*          height: 'auto', */
            resizable: false,
            open: function(){
                $.getScript('/assets/modal/in-modal-open.js');
            },
            close: function(event, ui){
                dialog.remove();
            }
        });
    });     
    return false;
});

However, I now need a page rendered by a Controller to display the same way, but there's no link to click that can be given the class .modal.

So, is there a way to call this JavaScript function from my Rails controller and pass it the proper parameters?

Upvotes: 0

Views: 1359

Answers (1)

Adam Templeton
Adam Templeton

Reputation: 4617

Figured it out!

I added a js file to my contact_messages view folder called create.js.erb.

Then I had my Contact Messages controller activate that js file with a respond_to block.

Inside the js file, I called a variation of the modal function...

Upvotes: 3

Related Questions