user1402072
user1402072

Reputation: 69

What's the proper way to render a rails action within a jquery modal window?

I'll try to keep my problem concise. So I'm trying to make a rails "render" call within a jquery modal window via jquery's .html function. But it's not working properly. The relevant controller is called users and its action is called new. Therefore the relevant jquery call I'm trying to make is .html('<%=render('users/new') %>') for the modal window. Here's my code.

        <script>
        $(document).ready(function(){
            var $dialog = $('<section></section>')
                                    .html('<%=render('users/new') %>')
                .dialog({
                    autoOpen: false,
                    modal:true
                });

            $('#target').click(function() {
                $dialog.dialog('open');
                return false;
            });
        });
        </script>

When I replaced the line .html('<%=render('users/new') %>') call with .html('<%=link_to("TEST", {:controller=>'users', :action=>'new'})%>') the window and link worked correctly. So I know the problem involves specifically the render call.

What can I do to fix this problem and allow render to work in a jquery modal window? Hope I was clear in my description. Thanks a lot for your help!

Upvotes: 0

Views: 359

Answers (1)

Ramandeep Singh
Ramandeep Singh

Reputation: 5253

Always use escape_javascript while using render in JS. Something like below will work:

<%= escape_javascript(render "users/new") %>

Upvotes: 1

Related Questions