1

I have a _slide.html.erb partial that is used in the page rendered by my SlidesController's index action. There are controls on this page that allow slides to be added via ajax. This means I need to add new slides to the view from my ajax response handlers.

How can I use my _slide.html.erb partial from this javascript handler to avoid duplication. I don't want to have a second version of the partial that is stored in and used by javascript.

1 Answer 1

1

you can render your partial inside your action.js.erb template:

$('#someelement').html('<%= escape_javascript(render :partial => "slide.html.erb", :locals => {:name => value}) %>');
Sign up to request clarification or add additional context in comments.

6 Comments

I get: undefined method `render' for #<#<Class:0x007f956b426cd0>:0x007f956a676360>
can you post the code? render should be available on views and controllers
I think the problem is that I'm using it in a js function that runs in response to a ui change rather than in a file rendered from a controller action. I am trying to append a partial to a list that is already in existance. Here is the code anyway:var slideDeck = $('#slide-deck'); slideDeck.append('<%= escape_javascript(render :partial => "slide") %>');
I don't understand, you do the request to some url that goes to some controller and renders some action in js format, how do you access that js file if it's not that? please put all the relevant code, I can't guess what you are doing
As I mention in my question, the page is rendered from an erb.html template. The javascript is loaded and on $(document).ready binds to various html elements. One of these elements is a button and clicking it triggers a $.ajax request to the controller. When this request fires the ajax success handler I need to add a slide to the page.
|

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.