簡體   English   中英

Rails 在 64ms 內完成 406 Not Acceptable

[英]Rails Completed 406 Not Acceptable in 64ms

我的網站上有一個表單,其中有一個頁面,我可以在其中編輯/刪除/添加郵箱:

每當我對郵箱執行某些操作(更新、銷毀)時,我都會收到此錯誤:

重定向到http://example.com/完成 406 Not Acceptable in 64ms

但是數據會更新。

這是控制器代碼:

# PUT /mailboxes/1
def update
  @mailbox = Mailbox.find(params[:id])

  if @mailbox.update_attributes(params[:mailbox])
    redirect_to(root_path, :notice => 'Mailbox was successfully updated.')
  else
    render :action => "edit"
  end
end

# DELETE /mailboxes/1
def destroy
  @mailbox = Mailbox.find(params[:id])
  @mailbox.destroy

  redirect_to(root_path)
end

這是 routes.rb 信息:

match 'settings.js' => 'settings#javascript', :via => :get, :format => :js
scope '/settings' do

  # Directs /settings/mailboxes/* to Settings::MailboxesController
  # (app/controllers/settings/mailboxes_controller.rb)
  resources :mailboxes     
end

我究竟做錯了什么? 以下是日志顯示的內容:

if @mailbox.update_attributes(params[:mailbox])
(rdb:2) response.status
200
(rdb:2) next
/Users/Fallen/Projects/support-app/trunk/app/controllers/mailboxes_controller.rb:65
redirect_to(mailboxes_path, :notice => 'Mailbox was successfully updated.') 
(rdb:2) response.status
200
(rdb:2) next
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.0/lib/action_controller/metal/implicit_render.rb:5
default_render unless response_body
(rdb:2) response_body
[" "]
(rdb:2) response.status
406
(rdb:2) cont


Started PUT "/settings/mailboxes/5" for 127.0.0.1 at 2011-10-14 12:54:38 +0200
  Status Load (0.4ms)  SELECT `statuses`.* FROM `statuses` WHERE `statuses`.`name` = 'Incoming emails fetching' LIMIT 1
   (0.1ms)  BEGIN
   (0.4ms)  UPDATE `statuses` SET `last_action_at` = '2011-10-14 10:54:38' WHERE `statuses`.`id` = 1
   (39.6ms)  COMMIT
  Processing by MailboxesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"0cip2dsYre9anfy/8rEgtuYcgrgC3si6aSuppjzxuHU=", "mailbox"=>{"name"=>"Dev Support #4", "sender_name"=>"example.com Support #4", "email_address"=>"support_dev4@example.com", "color"=>"B2EB3D"}, "commit"=>"Update Mailbox", "id"=>"5"}
  Mailbox Load (0.5ms)  SELECT `mailboxes`.* FROM `mailboxes` WHERE `mailboxes`.`id` = 5 LIMIT 1
  Status Load (0.5ms)  SELECT `statuses`.* FROM `statuses` WHERE `statuses`.`name` = 'Incoming emails fetching' LIMIT 1
   (0.1ms)  BEGIN
   (0.3ms)  UPDATE `statuses` SET `last_action_at` = '2011-10-14 10:54:47' WHERE `statuses`.`id` = 1
   (0.4ms)  COMMIT
   (0.2ms)  BEGIN
   (0.6ms)  UPDATE `mailboxes` SET `color` = 'B2EB3D', `updated_at` = '2011-10-14 10:54:48' WHERE `mailboxes`.`id` = 5
  Mailbox Load (0.7ms)  SELECT `mailboxes`.* FROM `mailboxes` 
   (1.2ms)  COMMIT
  Mailbox Load (0.4ms)  SELECT id, name, open_tickets_count FROM `mailboxes` 
   (0.3ms)  SELECT COUNT(*) FROM `tickets` WHERE `tickets`.`closed` = 0
  CACHE (0.0ms)  SELECT COUNT(*) FROM `tickets` WHERE `tickets`.`closed` = 0
Redirected to http://localhost:3000/settings/mailboxes
Completed 406 Not Acceptable in 29008ms

當您請求的內容類型不是操作知道如何響應的內容類型時,就會發生 406。 例如,如果一個動作響應 html 和 json,但你請求 js,那么它不會知道如何響應。

如果我沒記錯的話,它會完成工作,但是當需要響應時,它會發回 406。

我沒有看到您的控制器中指定了任何格式,但也許您在頂部使用respond_to指定了它們,或者我忘記了有關默認行為的某些內容。

看起來您正在請求 js -- 作為實驗,請嘗試將其包裝在您的整個操作中:

respond_to do |format|
  format.js do
    # all your action code goes here...
  end
end

測試在你的 routes.rb 中添加defaults format: :json

# config/routes.rb
defaults format: :json do
  # Your json routes here
  # resources :example ... 
end

此外,如果您正在使用responders gem,請確保在您的application_controller.rb中頂部有一個respond_to :json

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM