Rendering partial in js.erb file

后端 未结 1 1544
清酒与你
清酒与你 2020-12-02 15:16

I\'m trying to create an ajax-based comment form which will update my comments list when submitted. Pretty basic stuff.

I have a partial comments/_single.html.

1条回答
  •  -上瘾入骨i
    2020-12-02 16:03

    When rendering partials inside Javascript code, you should use the escape_javascript method, like so

    $('#comments ul.comments').append("<%= escape_javascript render(:partial => 'comments/single', :locals => { :c => @comment }) %>");
    

    This method is aliased as j, so you can also do this

    $('#comments ul.comments').append("<%= j render(:partial => 'comments/foobar') %>");
    

    The method Escapes carriage returns and single and double quotes for JavaScript segments ActionView::Helpers::JavaScriptHelper Rails docs

    0 讨论(0)
提交回复
热议问题