如何将渲染部分追加到视图类?

时间:2015-02-03 06:22:15

标签: jquery ruby-on-rails

我使用以下内容在views / posts / type.js.erb中创建了一个type.js.erb:

$('.more').on('click', function(){
    $('.posts').append("<%= render partial: 'type', locals: { count: 30 } %>");
})

它会在控制台中打印下一个错误:

Resource interpreted as Script but transferred with MIME type text/html: "http://178.62.102.154/".

178.62.102.154/:1 Refused to execute script from 'http://178.62.102.154/assets/type.js.erb' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

jquery.js:4 Resource interpreted as Image but transferred with MIME type text/html: "http://178.62.102.154/".

如何修复它并将其附加到.more div?

更新

index.html.erb =&gt; http://pastebin.com/jdRmDjDa

Posts_Controller =&gt; http://pastebin.com/HraLpeVg

_type.html.erb =&gt; http://pastebin.com/gmv0B15a

type.js.erb =&gt;我在上面写了

2 个答案:

答案 0 :(得分:0)

为此目的有一个帮助方法escaoe_javascript。 &lt;%= escape_javascript(render'form',:products =&gt; @products)%&gt; 您不必提及本地人直接传递它们

答案 1 :(得分:0)

似乎是我的预感,问题出在控制器中的type方法中:

def type
  render partial: "type", locals: { count: 30 }
end

这是做什么的,你正在尝试渲染一个html页面,但rails希望你渲染一个javascript。移除render partial: "type", locals: { count: 30 }并将type.js.erb重构为:

$('.more').on('click', function(){
  $('.posts').append("<%=j render 'type', count: 30 %>");
})

jescape_javascript的简写方法。尝试一下。