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


http://example.com/settings/mailboxes
http://example.com/settings/mailboxes/3/edit
http://example.com/settings/mailboxes/3
等等


每当我对邮箱执行某项操作(更新,销毁)时,都会出现此错误:


重定向到http://example.com/
在64ms内完成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"=>"[email protected]", "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

关于ruby-on-rails - Rails完成406在64ms内 Not Acceptable ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7752919/

10-11 02:46