3

我在生产服务器中遇到了错误“已完成 406 不可接受”,而我在暂存服务器中没有遇到该错误。

相同的代码在暂存中运行良好,但在生产中无法运行。两个环境都安装了相同的 gem。我使用的是 3.0.10 版本的导轨。

在我的生产环境中,GET 请求被处理为 html,但在暂存时它被处理为 js。我已经为 js 格式定义了 response_to 块。我不确定为什么它在两种环境中表现不同。

我也尝试在 routes.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

这让我很生气。感谢你的帮助。

路由.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

`

4

2 回答 2

7

经过大量调试,我终于得到了解决方案..

当服务器返回 Javascript 响应时,我的请求接受标头期待 HTML,因此浏览器无法接受。

在我的例子中,一个 Javascript 文件缺少 application.html.erb 中的 javascript_include_tag 。缺少的 Javascript 是jquery_ujs.js 。基本上,这设置了 ajax 加载的接受标头。此标记中包含的 Javascript 将被缓存和存储。所以我添加了丢失的 Javascript 并删除了缓存的文件夹和 YIPPEE,我的问题得到了解决。

我将举一个我从博客中学到的小例子。似乎对其他人理解 406 错误会有所帮助。

406错误可以理解为:假设我要你看书,但我只能看英文和法文的书。如果您只有德语书籍,您会给我一个“406 - 不可接受”状态的空回复。

406 是服务器告诉你“我有你要求的,但你不会接受”。

这就是 http 请求中的 Accept 标头的用途。例如,如果 HTTP 请求包含它只接受“img/jpg”MIME 类型的指示,如果请求的文件是 Excel 电子表格,服务器将发送 406 状态。

诊断:

  • 请求这些页面之一并分析 http 请求标头
  • 注意响应的 MIME 类型、字符集、GZIP 编码*

“406 不可接受”是一种不常见的状态码 - 最常见的是 200、404、500、301。当服务器出现问题时,您只会看到 406,通常是一些愚蠢但难以诊断的问题。

于 2013-08-16T12:40:45.890 回答
0

我有同样的问题。我的问题是我正在发出 HTML 请求,但我的 respond_to 块只接受 js。

所以我需要改变这个:

respond_to do |format|
  format.js
end

对此:

respond_to do |format|
  format.html
  format.js
end

然后这一切又开始了。

于 2018-06-30T14:23:31.340 回答