Skip to content Skip to sidebar Skip to footer

Append A Partial With Onclick Event

I have my onclick event working but when it comes to displaying the partial it: only displays the text: <%= escape_javascript(render(:partial => payment)) %> I want it to

Solution 1:

Based on the 'full jquery' code you have above I'm guessing you have this in a coffeescript file, which does not have access to the render method.

You're going to have to set up a route that hits a controller action that renders the JS, like this:

routes.rb:

get 'controller_name/toggle_partial' => "controller_name#toggle_partial"

In that controller:

def toggle_partial
  respond_to do |format|
  format.js
end

And in views/controller_name add a file called toggle_partial.js with these contents:

$(".payment").append("<%= escape_javascript(render(:partial => payment)) %>");

Then in your coffeescript file do something like this:

$('.plans').change ->
  $.ajax(
    type: 'GET'
    url: '/controller_name/toggle_partial'
    success: ( data, status, xhr ) ->
  )

Sorry if the indentation is off on the coffeescript example. The bottom line is that you don't have access to render in the coffeescript file so you have to make a work-around. Hope this helps.


Post a Comment for "Append A Partial With Onclick Event"