Rails *Completed 406 Not Acceptable\" in production ..working in Staging

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

I am stuck with an error "Completed 406 Not Acceptable" in the production server which I am not facing in the staging server .

Same code is working fine in staging but not working in production .Both environment has same gems installed . I am using a rails version 3.0.10.

In my production environment , the GET request is processed as a html but in staging it is processed as js .I have defined the respond_to block only for js format . I am not sure why it is behaving differently in two environments.

Also I have tried adding the defaults format as js in the routes.rb but that also is not working.

match 'releases/cycle_paginate', :to => 'releases#cycle_paginate', :defaults => { :format => 'js' } 

But this is also not working .

Below is the log for production and staging .

Production:

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

Staging:

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

Controller:

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 

This is making me mad. Appreciate your help.

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 

`

回答1:

Finally I got the solution after a lot of debugging ..

My request accept header was expecting a HTML while server was returning a Javascript response so that was not acceptable by browser.

In my case a Javascript file was missing the javascript_include_tag in the application.html.erb .The Javascript missing was jquery_ujs.js .Basically this sets the accept header for ajax loading .The Javascripts included in this tag will be cached and stored. So I added the missing Javascript and removed the cached folder and YIPPEE, my problem got resolved.

I will give a small example that I learnt somewhere from a blog. It seems that it will be helpful for others to understand the 406 error.

406 error can be understood as below: Suppose I've asked you for a book to read, but I can only read books in English and French. If you only have books in German, you would give me an empty response with a "406 - not acceptable" status.

406 is the server telling you "I have what you asked for, but you won't accept them".

That's what Accept headers in the http request are for. For instance, if the HTTP request includes directions that it will only accept "img/jpg" MIME type, the server would send a 406 status if the file requested is an Excel spreadsheet.

To diagnose:

  • request one of these pages and analyze the http request headers
  • Pay attention to the MIME type of the responses, the Character Set, GZIP encoding*

"406 not acceptable" is an unusual status code - the most common are 200, 404, 500, 301. You only see a 406 when something is wrong with the server, usually something silly but hard to diagnose.



回答2:

I had the same problem. My issue was that I was making an HTML request, but my respond_to block was only accepting js.

so I needed to change this:

respond_to do |format|   format.js end 

to this:

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

And then it all worked again.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!