在我的 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";
}
});