渲染JS.ERB会产生原始代码 [英] Rendering JS.ERB results in raw code

查看:57
本文介绍了渲染JS.ERB会产生原始代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行AJAX请求时, show.js.erb 呈现部分 _article.haml

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

我希望能够在 show.js.erb 中写下:

<%= j render  'article' %>

因为它有 .js 扩展名我我需要用JavaScript包装它(上面的例子不会渲染部分),所以:

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' %>');

这将呈现部分但带有
原始代码 - 包括HTML和JS转义。

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

welcome#index:

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

articles.js

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!")
    }
  })
})


推荐答案

这个答案属于@MrYoshiji。

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 直接渲染 _article 部分:

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) } }

这篇关于渲染JS.ERB会产生原始代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆