我在生产服务器中遇到了错误“ Completed 406 Not Acceptable”,在登台服务器中没有遇到该错误。
相同的代码可以在暂存阶段正常工作,但不能在生产环境中运行。两个环境都安装了相同的gem。我使用的是Rails 3.0.10版本。
在我的生产环境中,GET请求被处理为html,但在过渡过程中被处理为js。我已定义了js格式的response_to块。我不确定为什么它在两种环境中的行为会有所不同。
我也尝试在route.rb中将默认格式添加为js,但这也无法正常工作。
match 'releases/cycle_paginate', :to => 'releases#cycle_paginate', :defaults => { :format => 'js' }
但这也不起作用。
以下是生产和暂存日志。
生产:
Started GET "/releases/cycle_paginate/2?id=4" for 10.30.10.67 at Wed Aug 14 07:13:45 -0400 2013
Processing by ReleasesController#cycle_paginate as JS
Parameters: {"id"=>"4", "page"=>"2"}
分期:
Started GET "/releases/cycle_paginate/2?id=53" for 10.30.10.67 at Wed Aug 14 06:42:45 -0400 2013
Processing by ReleasesController#cycle_paginate as HTML
Parameters: {"id"=>"53", "page"=>"2"}
控制器:
def cycle_paginate
@release = Release.find(params[:id])
@cycles = []
@cycles = Kaminari.paginate_array(@release.cycles) \
.page(params[:page]) \
.per(5)
last_page = @cycles.num_pages
if params[:page].to_i > last_page
@cycles = @cycles.page(last_page)
else
@cycles = @cycles.page(params[:page].presence || 1)
end
respond_to do |format|
format.js
end
end
这让我发疯。感谢你的帮助。
Routes.rb
`Tcm::Application.routes.draw do match "login", :controller => "user_sessions", :action => "create", :as => "login"
match "logout", :controller => "user_sessions", :action => "destroy", :as => "logout"
resources :ic_tree_items do
get :children, :on => :member
get :nonmem_rel_children, :on => :member
get :release_children, :on => :member
get :cycle_children, :on => :member
end
resources :users
resources :ic_revisions
resources :tester_release_assignments
resources :roles
resources :project_platform_config_assignments
resources :platform_configs
resources :user_sessions
match 'ic_runs/execute_multiple', :to => 'IcRuns#execute_multiple', :as => "execute_multiple_ics", :via => :put
resources :ic_runs
resources :reports
resources :testruns
match "cycles/add_ics", :to => "cycles#add_ics"
resources :cycles
resources :releases do
get :autocomplete_user_name, :on => :collection
end
resources :projects
resources :tasks
resources :steps
resources :asserts
match 'releases/mass_assign', :to => 'Releases#mass_assign', :as => 'mass_assign_releases', :via => :post
match 'ics/mass_action', :to => 'Ics#mass_action', :as => 'mass_action_ics', :via => :post
match 'ics/filter' ,:to=>'Ics#filter',:via=> :post
match 'taggings/selection_options', :to => 'Taggings#selection_options', :as => 'selection_option_taggings',:via => :post
match 'tags/filter',:to=>'Tags#filter',:via=> :post
resources :ics
resources :tags
root :to => "Home#index"
match ':name' => 'Projects#show'
match 'ic_revisions/update', :to => 'ic_revisions#update'
match 'ic_revisions/clone_task', :action => "clone_task", :controller=>"ic_revisions"
match 'ic_revisions/definition_textbox', :to => 'ic_revisions#definition_textbox'
match 'tasks/destroy', :action => "destroy", :controller=>"tasks"
match 'steps/destroy', :action => "destroy", :controller=>"steps"
match 'asserts/destroy', :action => "destroy", :controller=>"asserts"
match 'cycles/:id/delete', :action => "destroy", :controller=>"cycles"
match 'ic_revisions/manage/:ic_id', :to => 'ic_revisions#index', :as => "manage_ic_revisions"
match 'cycles/:id/add_ics', :to => 'cycles#add_ics'
match 'cycles/del_cycle_ic', :to => 'cycles#del_cycle_ic'
match 'cycles/add_ic_run', :action => "add_ic_run", :controller=>"cycles", :via => :post
match 'releases/:id/add_ics', :to => 'releases#add_ics'
match 'users/lead/:id', :to => 'users#show_lead', :as => "lead"
match 'users/tester/:id', :to => 'users#show_tester', :as => 'tester'
match 'users/edit/:id', :to => 'users#edit', :as => 'users_edit'
match 'cycles/add_cq', :to => 'cycles#add_cq',:as=>'cycles_add_cq'
match 'ic_runs/update_ics', :to => 'ic_runs#update_ics'
match 'ic_runs/cycle/:cycle_id', :to => 'ic_runs#cycle', :as => 'cycle_ic_runs'
match 'ic_runs/edit_result', :to => 'ic_runs#edit_result'
match 'ic_runs/ic_run_list/:id', :to => 'ic_runs#ic_run_list', :as => 'ic_runs_list'
match 'reports',:to=>'reports#index'
match 'reports/update_releases', :to=>'reports#update_releases'
match 'reports/update_cycles', :to=>'reports#update_cycles'
match 'reports/show', :to=> 'reports#export_reports'
match 'ics/on_update_root', :to=>'ics#on_update_root'
match 'ic_runs/export_all', :to => 'ic_runs#export_all'
match 'ics/on_update_suite', :to=>'ics#on_update_suite'
match 'ic_runs/destroy', :to => 'ic_runs#destroy', :key=>:ic_run_ids
match 'cycles/:id/clone', :action => "clone", :controller=>"cycles"
match 'releases/:id/clone', :action => "clone", :controller=>"releases"
match 'cycles/:id/delete_ics', :action => "delete_ics", :controller=>"cycles"
match 'releases/update_testers', :to => 'releases#update_testers'
match 'releases/update_testers(/:page)', :action => "update_testers", :controller=>"Releases", :page => :page
match 'releases/cycle_paginate', :to => 'releases#cycle_paginate'
match 'releases/cycle_paginate(/:page)', :action => "cycle_paginate", :controller=>"Releases", :page => :page
match 'ics/new_ics', :to =>'ics#', :action => "new_ics", :controller => "Ics"
match 'ics/obsolete_ics', :to=>'ics#', :action=>"obsolete_ics", :controller=>"Ics"
match 'ics/new_ic_stub', :to=>'ics#', :action=>"new_ic_stub", :controller=>"Ics"
match 'ics/index', :to=>'ics#', :action=>"index", :controller=>"Ics"
match 'releases/rel_all_ics', :action => "rel_non_member", :controller => "Releases"
match 'cycles/cycle_non_member', :action=>"cycle_non_member", :controller=>"Cycles"
end
`
最佳答案
经过大量的调试,终于得到了解决方案..
我的请求接受标头在服务器返回Javascript响应时期望HTML,因此浏览器不接受。
在我的情况下,一个Javascript文件在application.html.erb中缺少javascript_include_tag。该Javascript缺少是jquery_ujs.js。基本上,这将设置ajax加载的accept标头。此标签中包含的Javascript将被缓存和存储。
所以我添加了缺少的Javascript并删除了缓存的文件夹和YIPPEE,我的问题得到解决。
我将举一个我从博客中学到的小例子。看来这对其他人理解406错误会有所帮助。
406错误可以理解如下:
假设我要你读书,但我只能读英语和法语的书。如果您只有德文书籍,您会给我一个空的答复,状态为“ 406-不可接受”。
服务器406告诉您“我有您要的内容,但您不接受”。
这就是http请求中的Accept标头所针对的。例如,如果HTTP请求包含仅接受“ img / jpg” MIME类型的说明,则如果请求的文件是Excel电子表格,则服务器将发送406状态。
诊断:
请求这些页面之一并分析http请求标头
注意响应的MIME类型,字符集,GZIP编码*
“ 406 not accept”是不寻常的状态码-最常见的是200、404、500、301。只有在服务器出现问题时(通常是愚蠢但难以诊断),您才会看到406。