1

在我的 Rails 应用程序中,javascript 模板profile.js.erb不会被渲染。我得到错误代码406 Not Acceptable该模板应该在视图star#profile中附加一个部分。Ajax 请求是由 jquery 无限滚动插件发出的。这是我的代码

.

行动

 def profile
 @page=params[:page] ||1
 @videos=Video.all(:page=>@page)
 respond_to do |format|
 format.js
 format.html
  end
end

查看星星#profile

<div id="content">
<div class="post">
<%=render 'videos'%>
</div>

profile.js.erb

  $('div.post').append("<%=escape_javascript(render 'videos')%>");

路线

      match "stars/profile/:page"=> "stars#profile", :via => :get

登录控制台

Started GET "/stars/profile?page=2" for 127.0.0.1 at 2013-06-20 00:05:58 -0500
Processing by StarsController#profile as application/JavaScript
Parameters: {"page"=>"2"}
Completed 406 Not Acceptable in 65836ms

Ajax 设置

   jQuery.ajaxSetup({ 
  'beforeSend': function(xhr, settings) {
   xhr.setRequestHeader("Accept", "application/javascript");
   var token=$('meta[name="csrf-token"]').attr('content');
   xhr.setRequestHeader('X-CSRF-Token',token );
   settings['dataType'] = "javascript";
   settings['contentType'] = "application/javascript";
    }

});

4

1 回答 1

0

从日志中: GET "/stars/profile?page=2" 在配置文件之后不包含 .js。所以rails不会在javascript中处理它。

GET "/stars/profile.js?page=2" 应该是调用..

于 2013-06-25T09:38:53.013 回答