17

I have a form on my website with a page where I can edit / remove / add mailboxes:

Whenever I do something to the mailbox (update, destroy) I'm getting this error:

Redirected to http://example.com/ Completed 406 Not Acceptable in 64ms

But the data gets updated.

Here's the controller code:

# 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

Here's routes.rb info:

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

What am I doing wrong? Here's what log shows:

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
Kenster
  • 23,465
  • 21
  • 80
  • 106
Thunder
  • 611
  • 1
  • 8
  • 25
  • Any clues guys? Expanded I log a bit. – Thunder Oct 14 '11 at 10:58
  • Hi Fallen - dont suppose you tracked down the problem? I am getting something very similar - although I am thinking it might be related to the csrf tag stuff - maybe its not getting sent through. – Chris Kimpton Nov 23 '11 at 22:43
  • It could be a question of mime type. If your browser expects to receive text/html and receives for example text/javacript, it could break the response and throw a 406. Try to add the mime type text/javascript if not added to config/initializers/mime_types.rb – Kzu Dec 12 '11 at 08:08
  • 1
    maybe this will help you: http://stackoverflow.com/questions/7808051/rails-completed-406-not-acceptable – Roger Dec 12 '12 at 20:52

3 Answers3

35

406 happens when the content type that you are requesting is not one that an action knows how to respond with. For example if an action responds to html and json, but you request js, then it won't know how to respond.

If I'm not mistaken, it will do the work, but when it comes time to respond it will send back a 406.

I don't see any formats specified in your controller, but maybe you have them specified at the top with respond_to, or maybe I'm forgetting something about default behavior.

It looks like you are requesting js -- as an experiment, try wrapping this around your entire action:

respond_to do |format|
  format.js do
    # all your action code goes here...
  end
end
John Bachir
  • 22,495
  • 29
  • 154
  • 227
0

Test adding the defaults format: :json in your routes.rb

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

Also, if you are using the responders gem, ensure that in your application_controller.rb you have a respond_to :json at the top.

user9869932
  • 6,571
  • 3
  • 55
  • 49
0

I had this error and ended up here after googling. In my case I was missing the template but did not receive the usual Missing Template Error. I believe it was because I had respond_to :html at the top of the controller.

abax
  • 727
  • 5
  • 9