1

在我的Rails应用程序中,javascript模板profile.js.erb不会呈现。我收到错误代码406 Not Acceptable。这个模板应该在视图中追加一个局部星号#profile。 Ajax请求由jquery无限滚动插件完成。这里是我的代码Ajax请求返回(406不可接受)

动作

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

视图星#轮廓

<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"; 
    } 

});

+0

通过StarController旁加工评论说轮廓JSON。看看你的控制器中的格式是什么,以确保它是html或js。如果格式是json,这将解释为什么你得到406,因为你的控制器没有处理该格式。 – Keith

+0

当我添加format.json,解决'406错误代码',但我需要*** profile.js.erb ***得到渲染不json – katie

+0

我认为你必须在您的请求中设置接受头'应用程序/ javascript',不知道该怎么做,虽然,检查http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings – juanpastas

回答

0

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

GET“/stars/profile.js?page=2”应该是通话..