Rendering JS.ERB results in raw code

advertisements

When an AJAX request executes, show.js.erb renders the partial _article.haml.

What I want to be able to do in show.js.erb is to write:

<%= j render  'article' %>

Since it has a .js extension I am required to wrap this in JavaScript (the example above does not render the partial), so:

'<%= j render  'article' %>' OR ('<%= j render  'article' %>');

This would render the partial but with raw code--including HTML and JS escaping.

('things will go back to \"normal.\"<\/p>\n\n');

What's the right way to do this?

welcome#index:

.ajax_load.article-content{ data: { 'remote-url' => article_path(@article) } }

articles.js:

 $(document).ready(function() {
  $('.ajax_load').each(function(index, element) {
    var url = $(element).data('remote-url')
    if (url) {
      $.get(url, function(responseText) {
        $(element).html(responseText);
      })
    } else {
      console.log("missing url for ajax!")
    }
  })
})


This answer belongs to @MrYoshiji.

Ajax:

  $(document).ready(function() {
      $('.ajax_load').each(function(index, element) {
        var url = $(element).data('remote-url')
        if (url) {
          $.get(url, function(responseText) {
            $(element).html(responseText);
          }, 'html' )
        } else {
          console.log("missing url for ajax!")
        }
      })
  })

articles_conroller renders _article partial directly:

def show
    #respond to JS
    respond_to do |format|
        format.js { render :partial => "article" }
    end
end

welcome#index:

 .ajax_load.article-content{ data: { 'remote-url' => article_path(@article) } }