Append a partial with onclick event

旧巷老猫 提交于 2019-12-24 13:02:09

问题


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 fetch the parial and display it

here is my code:

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

tried with "" and '' around payment

full jquery code for this

$(document).ready ->
  $('.plans').change ->
    $(".payment").append("<%= escape_javascript(render(:partial => 'payment')) %>");

edit:

this worked for me if anyone looks at this in the future since i use devise:

  devise_scope :user do
    get 'registrations/toggle_partial' => "registrations#toggle_partial"
  end

put the toggle_partial.js file in

views/devise/registrations

everything else that NickM put there is great....

also remember the '' around the partial name


回答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.



来源:https://stackoverflow.com/questions/26537575/append-a-partial-with-onclick-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!