0

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?

1
  • Do a response format js front the controller, and then create a js view with the escape_javascrit... Commented Feb 2, 2015 at 21:49

2 Answers 2

3

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") %>');
Sign up to request clarification or add additional context in comments.

2 Comments

# controller_action.js.haml - shouldn't that be erb?
LOL ... yes, if you use ERB and not HAML ... I hate ERB since it reminds me of ASP and makes cold shivers go down my spine ;) ... but you're right as his escape_javascript is using ERB. Will edit
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.