mikeymurph77
mikeymurph77

Reputation: 802

Rendering a Rails partial on a jQuery click function

I may be thinking of this all wrong but I've been trying to render a partial in a jQuery click function like so...

closeShow.click(function(){
    parentDiv.empty();
    parentDiv.html('<%= escape_javascript(render "sibling_div_partial") %>');
});

I've also tried using .append for the partial but the when I check the view in the browser, it comes up with just the text (i.e <%= escape_javascript(render "sibling_div_partial") %>) instead of the partial.

A little more infomration as to what I'm trying to accomplish.... I want to remove a div when the close button is clicked and then rebuild that div as a partial when a close button is clicked.

Am I conceptualizing this all wrong?

Upvotes: 0

Views: 1794

Answers (2)

craig.kaminsky
craig.kaminsky

Reputation: 5598

Were it me, I would probably go with a remote link in Rails, which makes a js-formatted request to your controller/action.

Once clicked, the remote link would hit the controller/action specified in the link path/url and then, in that controller action's js file you can run the appropriate JavaScript.

The ERB/HAML View

# home/index.html.erb 
<%= link_to 'Some Link', some_controller_path, remote: true %>

The Controller

# my controllers_controller.rb
def controller_action
  # do awesome controller stuff
end 

The Action's JS View

# controller_action.js.erb 
$('parent-div').empty();
$('parent-div').html('<%= escape_javascript(render "sibling_div_partial") %>');

Upvotes: 3

Xianny
Xianny

Reputation: 107

I don't know if you can render dynamically. Rendering happens in the controller, whereas javascript is all view (i.e. client-side).

You can try putting the partial in your template, and toggle visibility of parentDiv when clicked.

Upvotes: 0

Related Questions