I have a form on my website with a page where I can edit / remove / add mailboxes:
- http://example.com/settings/mailboxes
- http://example.com/settings/mailboxes/3/edit
- http://example.com/settings/mailboxes/3
- etc.
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/开发者_StackOverflow社区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 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
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.
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.
精彩评论