【问题标题】:Rails Completed 406 Not Acceptable in 64msRails Completed 406 Not Acceptable in 64ms
【发布时间】:2011-10-13 10:43:03
【问题描述】:

我的网站上有一个表单,其中包含一个可以编辑/删除/添加邮箱的页面:

每当我对邮箱执行某些操作(更新、销毁)时,我都会收到此错误:

重定向到http://example.com/ 在 64 毫秒内完成 406 不可接受

但是数据会更新。

这是控制器代码:

# 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

【问题讨论】:

  • 有什么线索吗?展开我记录了一下。
  • 嗨,Fallen - 不要以为您找到了问题所在?我得到了一些非常相似的东西——尽管我认为它可能与 csrf 标签的东西有关——也许它没有被发送出去。
  • 这可能是 mime 类型的问题。如果您的浏览器希望接收 text/html 并接收例如 text/javacript,它可能会中断响应并抛出 406。如果未添加到 config/initializers/mime_types.rb,请尝试添加 mime 类型 text/javascript >
  • 也许这会对你有所帮助:stackoverflow.com/questions/7808051/…

标签: ruby-on-rails ruby-on-rails-3 routes


【解决方案1】:

当您请求的内容类型不是某个操作知道如何响应的内容类型时,就会发生 406。比如一个action响应html和json,但是你请求js,那么它就不知道怎么响应了。

如果我没记错的话,它会工作,但是当需要响应时,它会发回 406。

我没有看到您的控制器中指定了任何格式,但也许您在顶部用respond_to 指定了它们,或者我可能忘记了一些关于默认行为的内容。

看起来您正在请求 js —— 作为一个实验,尝试将其包裹在您的整个操作中:

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

【讨论】:

    【解决方案2】:

    测试在你的 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

    【讨论】:

      【解决方案3】:

      我遇到了这个错误,并在谷歌搜索后结束了。在我的情况下,我缺少模板,但没有收到通常的缺少模板错误。我相信这是因为我在控制器顶部有respond_to :html

      【讨论】:

        猜你喜欢
        • 2013-08-10
        • 1970-01-01
        • 2011-08-16
        • 2017-05-25
        • 2018-07-31
        • 2016-12-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-21
        相关资源
        最近更新 更多