iamtoc
iamtoc

Reputation: 1709

Ruby Rails 2.3.x How to load a partial on click event

How would one load the partial ('reports/form') into the hidden div (report_mod) when the image (category_icon) is clicked?. Currently, the partial is loading when the page loads.

Just to be clear, the goal is to prevent the partial from loading until the image icon is clicked.

If the use of an iframe is necessary to do this, thats ok too. I would just need to know how to reference the path from the partial, where the form may be a new record or an edit of an existing record. Is link_to_remote what I'm looking for? and if so how would I write that?

$('#category_icon').click(function(){
    $('#report_mod').html("<%= escape_javascript(render(:partial => 'reports/form')) if current_user %>");
    $('#report_mod').toggle('medium');      
});

Upvotes: 1

Views: 429

Answers (1)

CupraR_On_Rails
CupraR_On_Rails

Reputation: 2489

You can use an ajax method to do this. for example:

#in your js file or view
$('#category_icon').click(function(){
    $.ajax(`{
        url: "/your/url/for/form",
        dataType: "html",
        type: "GET",
        success: function(html) {          
            $('#report_mod').html(html);
            $('#report_mod').show();
        }`
    }); 
});
#in your controller
def display_form
    @your_object = YourObject.new
    respond_with do |format|
        format.html { }
        format.js { 
            render :partial => "reports/form"
        }
    end
end

And You need to add a route for this action in your config/routes.rb as a collection.

I hope this help.

Upvotes: 2

Related Questions