Rails 3.2 update boolean value from checkbox not applying - ruby-on-rails

I have an issue whereas I am trying to update a boolean field in the database with true or false based off of a checkbox.
I am using rails 3.2.2, using RVM I tried with 3.2.9 with the same result. This is in development, and I'm running postgresql as my database.
I am running in development so I am not using attr_accessible until I get to a later testing date. All similar questions I have seen on here seem to be related to forgetting to add the boolean field to attr_accessible, so letting everyone know up front that's not the issue.
From a functional standpoint this piece of the app is basically a list of events (or tasks) that the user can schedule. There is an index page that will pull a list of all the tasks they have for the day. The user can then click the checkbox next to the task and it will update the task as completed using the boolean field in question via an ajax call.
Here is the pertinent section of the index view:
=render :partial => "mktg/home/tasks"
And here is the partial _tasks.html.haml
#mktg_home_tasks
- #tasks = Mktg::Event.where('DATE(starts_at) = DATE(?)', Date.today).all
- if #tasks.empty?
You currently have no tasks...
- else
- #tasks.each do |t|
= form_for t, :index => t.id, :remote => true do |task|
= task.check_box :completed
- if task.object.completed == true
= link_to task.object.title,{}, :class => 'strike_through'
- else
= link_to task.object.title
The update method of the events controller is basically scaffolded:
def update
#event = Mktg::Event.find(params[:id])
respond_to do |format|
if #event.update_attributes(params[:event])
format.js
format.html { redirect_to #event, :notice => 'Event was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => #event.errors, :status => :unprocessable_entity }
end
end
end
Here is the update.js.coffee:
jQuery ->
$('#mktg_home_tasks').empty()
$('#mktg_home_past_due').empty()
$('#mktg_home_tasks').append("<%= escape_javascript(render 'mktg/home/tasks')%>")
I have the following asset running that fires off the form submittal via jquery for the checkbox:
EDIT: from first comment below my post had incorrect indentation below.
jQuery ->
$(":checkbox").live "change", ->
$(this).parents("form:first").submit()
When I run the app as is, no values get committed to the database. The console dump looks like this:
Started PUT "/mktg/events/133" for 127.0.0.1 at 2012-12-10 17:21:42 -0800
Processing by Mktg::EventsController#update as JS
Parameters: {"utf8"=>"✓", authenticity_token"=>"5ahLYhEjY2QEh6G0XOrathPXMSMaiDoCAgABpDbCZck=", "mktg_event"=>{"133"=>{"completed"=>"1"}}, "id"=>"133"}
Mktg::Event Load (0.3ms) SELECT "mktg_events".* FROM "mktg_events" WHERE "mktg_events"."id" = $1 ORDER BY starts_at ASC LIMIT 1 [["id", "133"]]
(0.2ms) BEGIN
(0.3ms) SELECT "mktg_leads".id FROM "mktg_leads" INNER JOIN "mktg_event_leads" ON "mktg_leads"."id" = "mktg_event_leads"."lead_id" WHERE "mktg_event_leads"."event_id" = 133
(0.2ms) COMMIT
Mktg::Event Load (0.5ms) SELECT "mktg_events".* FROM "mktg_events" WHERE (DATE(starts_at) = DATE('2012-12-10')) ORDER BY starts_at ASC
Rendered mktg/home/_tasks.html.haml (3.4ms)
Rendered mktg/events/update.js.coffee (4.9ms)
Completed 200 OK in 66ms (Views: 6.0ms | ActiveRecord: 4.8ms)
So I then tried adding the following before_save method to the Event model:
def set_completed_value
if self.completed == 0
self.completed = false
else
self.completed = true
end
end
With this added I am able to select the events as being completed and it works by updating the database field with true, here is the console output from that:
Started PUT "/mktg/events/133" for 127.0.0.1 at 2012-12-10 16:48:59 -0800
Processing by Mktg::EventsController#update as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"5ahLYhEjY2QEh6G0XOrathPXMSMaiDoCAgABpDbCZck=", "mktg_event"=>{"133"=>{"completed"=>"1"}}, "id"=>"133"}
Mktg::Event Load (0.3ms) SELECT "mktg_events".* FROM "mktg_events" WHERE "mktg_events"."id" = $1 ORDER BY starts_at ASC LIMIT 1 [["id", "133"]]
(0.1ms) BEGIN
(0.4ms) SELECT "mktg_leads".id FROM "mktg_leads" INNER JOIN "mktg_event_leads" ON "mktg_leads"."id" = "mktg_event_leads"."lead_id" WHERE "mktg_event_leads"."event_id" = 133
(0.3ms) UPDATE "mktg_events" SET "completed" = 't', "updated_at" = '2012-12-11 00:48:59.877945' WHERE "mktg_events"."id" = 133
(0.5ms) COMMIT
Mktg::Event Load (0.3ms) SELECT "mktg_events".* FROM "mktg_events" WHERE (DATE(starts_at) = DATE('2012-12-10')) ORDER BY starts_at ASC
Rendered mktg/home/_tasks.html.haml (29.4ms)
Rendered mktg/events/update.js.coffee (31.0ms)
Completed 200 OK in 36ms (Views: 31.8ms | ActiveRecord: 1.8ms)
But when I deselect the checkbox to mark a completed event as not completed the database is not updated. Here is the console output:
Started PUT "/mktg/events/133" for 127.0.0.1 at 2012-12-10 16:49:14 -0800
Processing by Mktg::EventsController#update as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"5ahLYhEjY2QEh6G0XOrathPXMSMaiDoCAgABpDbCZck=", "mktg_event"=>{"133"=>{"completed"=>"0"}}, "id"=>"133"}
Mktg::Event Load (0.2ms) SELECT "mktg_events".* FROM "mktg_events" WHERE "mktg_events"."id" = $1 ORDER BY starts_at ASC LIMIT 1 [["id", "133"]]
(0.2ms) BEGIN
(0.3ms) SELECT "mktg_leads".id FROM "mktg_leads" INNER JOIN "mktg_event_leads" ON "mktg_leads"."id" = "mktg_event_leads"."lead_id" WHERE "mktg_event_leads"."event_id" = 133
(0.1ms) COMMIT
Mktg::Event Load (0.4ms) SELECT "mktg_events".* FROM "mktg_events" WHERE (DATE(starts_at) = DATE('2012-12-10')) ORDER BY starts_at ASC
Rendered mktg/home/_tasks.html.haml (3.4ms)
Rendered mktg/events/update.js.coffee (4.8ms)
Completed 200 OK in 9ms (Views: 5.5ms | ActiveRecord: 1.2ms)
I've never had to add a before_save method like that to get a checkbox to update a boolean value in the database. So I must be doing something wrong. I know I'm close. If anyone has help or suggestions I would really appreciate it.
Thank you...
EDIT: Trying the answer given by John Naegle
I changed
= form_for t, :index => t.id, :remote => true do |task|
To
= form_for t, :index => t.id, :as => :event, :remote => true do |task|
And I get the following console output
Started PUT "/mktg/events/133" for 127.0.0.1 at 2012-12-11 09:48:14 -0800
Processing by Mktg::EventsController#update as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"5ahLYhEjY2QEh6G0XOrathPXMSMaiDoCAgABpDbCZck=", "event"=>{"133"=>{"completed"=>"1"}}, "id"=>"133"}
Mktg::Event Load (0.2ms) SELECT "mktg_events".* FROM "mktg_events" WHERE "mktg_events"."id" = $1 ORDER BY starts_at ASC LIMIT 1 [["id", "133"]]
(0.1ms) BEGIN
(0.1ms) ROLLBACK
Completed 500 Internal Server Error in 2ms
ActiveRecord::UnknownAttributeError (unknown attribute: 133):
app/controllers/mktg/events_controller.rb:89:in `block in update'
app/controllers/mktg/events_controller.rb:88:in `update'
The record ID I am trying to change is 133 (which is the 'unknown attribute' above).
Here is the Update method with line numbers
#85 def update
#86 #event = Mktg::Event.find(params[:id])
#87
#88 respond_to do |format|
#89 if #event.update_attributes(params[:event])
#90 format.js
#91 format.html { redirect_to #event, :notice => 'Event was successfully updated.' }
#92 format.json { head :no_content }
#93 else
#94 format.html { render :action => "edit" }
#95 format.json { render :json => #event.errors, :status => :unprocessable_entity }
#96 end
#97 end
#98 end
Anything else I should try, or do you see any error in my code. Thanks again.
EDIT: Using John Naegle's answer from below I now have this working
= form_for t, :index => t.id, :as => :event, :remote => true do |task|
The above was causing the following console output and subsequent error:
"event"=>{"133"=>{"completed"=>"1"}}, "id"=>"133"}
By getting rid of :index the record ID stopped being passed incorrectly. Once I changed to this:
= form_for t, :as => :event, :remote => true do |task|
Everything started working correctly. Thanks for the help! I don't know how I'd get "unstuck" sometimes if it wasn't for this site and everyone who helps. I love this place.

It looks to me like your controller does this:
#event.update_attributes(params[:event])
But your post parameters are:
"utf8"=>"",
"authenticity_token"=>"5ahLYhEjY2QEh6G0XOrathPXMSMaiDoCAgABpDbCZck=",
"mktg_event"=>{"133"=>{"completed"=>"0"}},
"id"=>"133"}
params[:event] is empty.

Related

Redirect after a particular action

I have been facing some problems with mailboxer gem . The reply and trash specifically , the messages are replied to and when selected to delete are sent to trash too but i haven't able to successfully redirect it to the inbox after it does so . I always keep getting the error "Cannot find user with id" Following is my controller code for the trash part -
def trash
conversation = Conversation.find(params[:id])
beta = current_userA || current_userB
case beta
when current_userA
conversation.move_to_trash(current_userA)
# redirect_to :conversations
when current_userB
conversation.move_to_trash(current_userB)
# redirect_to :conversations
end
respond_to do |format|
format.html { redirect_to conversation_path, notice: 'Conversation successfully trashed.' }
end
end
Logs
Started GET "/conversations/36" for 127.0.0.1 at 2014-08-28 10:19:56 +0530
Processing by ConversationsController#show as HTML
Parameters: {"id"=>"36"}
Message Load (0.5ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."type" IN ('Message') AND "notifications"."id" = $1 LIMIT 1 [["id", "36"]]
Conversation Load (0.4ms) SELECT "conversations".* FROM "conversations" WHERE "conversations"."id" = 34 LIMIT 1
Completed 404 Not Found in 4ms
ActiveRecord::RecordNotFound (Couldn't find userA without an ID):
app/controllers/conversations_controller.rb:160:in `show'
Rendered /usr/local/rvm/gems/ruby-1.9.3-p545/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.2ms)
Rendered /usr/local/rvm/gems/ruby-1.9.3-p545/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered /usr/local/rvm/gems/ruby-1.9.3-p545/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (19.7ms)
Updated Log
After changing redirect_to conversation with redirect_to conversations
Started POST "/conversations/38/trash" for 127.0.0.1 at 2014-08-28 12:29:16 +0530
Processing by ConversationsController#trash as HTML
Parameters: {"authenticity_token"=>"CuXzMKNmD87qvfOuer4uhyTdysFS/5NxtfMZU3/Y5j0=", "id"=>"38"}
Conversation Load (0.4ms) SELECT "conversations".* FROM "conversations" WHERE "conversations"."id" = $1 LIMIT 1 [["id", "38"]]
Filmmaker Load (0.7ms) SELECT "userA".* FROM "userA" WHERE "userA"."id" = 67 LIMIT 1
Receipt Load (0.8ms) SELECT "receipts".* FROM "receipts" INNER JOIN "notifications" ON "notifications"."id" = "receipts"."notification_id" AND "notifications"."type" IN ('Message') WHERE "notifications"."conversation_id" = 38 AND "receipts"."receiver_id" = 67 AND "receipts"."receiver_type" = 'userA'
SQL (11.3ms) UPDATE "receipts" SET "trashed" = 't' WHERE (id = 82 )
User Load (29.7ms) SELECT "users".* FROM "users"
Completed 500 Internal Server Error in 110ms
NameError (undefined local variable or method `conversations' for #<ConversationsController:0x10961634>):
app/controllers/conversations_controller.rb:101:in `block (2 levels) in trash'
app/controllers/conversations_controller.rb:100:in `trash'
Please do advice what i am doing wrong , i have been at it since a day and i haven't found any solution to it .Needless to say , userA and userB are both different devise users. Any suggestions on it is most welcome .
Try to use a path in redirect, which you can get from rake_routes. Maybe the example given below will work for you.
respond_to do |format|
format.html { redirect_to inbox_path, notice: 'Conversation successfully trashed.' }
end

Rails record from AJAX post not saving

I have an HTML table that displays order items. On each table row I have a "Need By Date" field that is standalone(not in a form) input field. Whenever a user changes the date, I post this date to the server to save it against the order.
I am noticing that when I use OrderItem.update or OrderItem.update_attributes from the rails console it works, but when it is triggered from a field change event Rails does not trigger an update to the table.
#JavaScript
$(".order-item.datepicker").change ->
needby_date = $(this).val()
order_item_url = $(this).data("order-item-url")
$.ajax
type: "POST"
url: order_item_url
data:
needby_date: needby_date
My controller -
def update_needby_date
#order_item = OrderItem.find(params[:order_item_id])
#order_item.update_attribute(update_needby_date_params)
respond_to do |format|
format.js { render :nothing => true }
end
end
private
def update_needby_date_params
params.permit(:order_item_id, :needby_date)
end
As you can see in the log file, the update is not being triggered -
Started POST "/order_items/1/update_needby_date" for 127.0.0.1 at 2014-07-08 10:55:10 -0400
Processing by OrderItemsController#update_needby_date as */*
Parameters: {"needby_date"=>"07/23/2014", "order_item_id"=>"1"}
OrderItem Load (0.4ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."id" = ? LIMIT 1 [["id", "1"]]
(0.4ms) begin transaction
(0.2ms) commit transaction
Rendered text template (0.1ms)
Completed 200 OK in 16ms (Views: 3.3ms | ActiveRecord: 1.0ms)

Get ID of submitted record after save in AngularJS Rails

Basically, here's my controller:
class Api::ProjectsController < ApplicationController
before_action :set_params, only: [:show]
respond_to :json
def new
#project = Project.new
render layout: false
end
def create
#project = Project.new allowed_params
#project.sample = false
#project.users << current_user
if #project.save
#project.members << current_user
# redirect_to project_path(#project)
else
end
render json: {new_project: #project}
end
def set_params
#page = params[:page]
#client = params[:client]
#block = params[:block]
#block = Block.find #block if #block
end
def allowed_params
params.require(:project).permit :name, :sample
end
end
This is my html snippet:
<form ng-submit='createStream()'>
<input autofocus='autofocus' class='form-control' id='project_name' placeholder='Untitled Stream' required='required' type='text', ng-model='newProject.name'>
<br/>
<input type='submit' value='Create' class='btn btn-md btn-success'>
</form>
My popovercontroller.js.coffee
App.controller("PopoverCtrl", ($scope, Project, $location, $http) ->
$scope.initializeProject = ->
$scope.newProject = new Project();
$scope.createStream = ->
project = Project.save($scope.newProject)
$scope.projects.push(project)
)
And the services/factory
App.factory 'Project', ($resource) ->
$resource "/api/projects/:id", {id: #id},
query:
method: "POST"
isArray: false
I can save data into my DB, but I want to redirect to the show view after saving the data.
Just like the default scaffolding for RAILS, wherein after submitting the form given that it is valid... it will redirect to its show page . (e.g. localhost:3000/projects/1 with its ID.
However, the behavior now is that after submitting the form, it's not redirecting to the show page. I'm trying to get the ID of the saved record but no luck.
I tried putting $location.path('/projects/' + #id) but seems like #id is undefined and not being recognized. I'm new in manipulating JSON using Angular.
EDIT: Here's my console logs. I think JSON isn't returning? It redirects to the page where I came from. My form is in popover style so no redirection has been made.
Started POST "/api/projects" for 127.0.0.1 at 2014-01-30 06:38:30 +0800
Processing by Api::ProjectsController#create as HTML
Parameters: {"name"=>"XXX", "project"=>{"name"=>"XXX"}}
User Load (2.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 14 ORDER BY `users`.`id` ASC LIMIT 1
(0.2ms) BEGIN
SQL (0.3ms) INSERT INTO `projects` (`created_at`, `name`, `updated_at`) VALUES ('2014-01-29 22:38:30', 'XXX', '2014-01-29 22:38:30')
(0.2ms) INSERT INTO `projects_users` (`project_id`, `user_id`) VALUES (91, 14)
(16.4ms) COMMIT
(0.2ms) BEGIN
SQL (0.3ms) INSERT INTO `project_memberships` (`member_id`, `project_id`) VALUES (14, 91)
(1.2ms) COMMIT
Completed 200 OK in 53ms (Views: 0.5ms | ActiveRecord: 22.3ms)
Started GET "/projects/23" for 127.0.0.1 at 2014-01-30 06:38:30 +0800
Started GET "/?goto=projects/23" for 127.0.0.1 at 2014-01-30 06:38:30 +0800
Processing by MainController#index as HTML
Parameters: {"goto"=>"projects/23"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 14 ORDER BY `users`.`id` ASC LIMIT 1
Rendered main/index.html.haml within layouts/main (0.1ms)
Rendered application/_header.html.slim (0.5ms)
Rendered application/_footer.html.slim (0.2ms)
Completed 200 OK in 40ms (Views: 36.0ms | ActiveRecord: 0.4ms)

Ruby class 'split' not working

I'm trying to use the split class to split up an array. I keep getting this error:
MethodError (undefined method `split' for nil:NilClass):
This is what I'm trying to do:
def create
#listing.landlord = current_landlord
if #listing.save
photo_id_array = params[:images].split(/,/)
photo_id_array.each do |image|
#image = Photo.find_by_id("photo.id")
#image.listing_id = #listing.id
end
render :show, :status => 200
else
render :status => 403, :json => {:errors => #listing.errors}
end
end
I'm running Rails 3.2.9. Any ideas into what may be causing this?
Edit:
Here is the full post:
Started POST "/listings" for 127.0.0.1 at 2012-12-12 01:04:52 -0800
Processing by ListingsController#create as application/json; charset=utf-8
Parameters: {"listing"=>{"street_address"=>"123 main st", "city"=>"los angeles", "state"=>"ca", "availability"=>"12 Dec 2012", "price"=>"2000", "period"=>"2", "category"=>"1", "cats"=>"false", "dogs"=>"false", "square_footage"=>"0", "bedrooms"=>"3", "bathrooms"=>"2", "short_description"=>"tree-lined street", "long_description"=>"big yard", "images"=>["70621", "70622", "70620"]}, "auth_token"=>"6489Cn7KeTmejSxaWsws"}
WARNING: Can't verify CSRF token authenticity
User Load (1.6ms) SELECT "users".* FROM "users" WHERE "users"."authentication_token" = '6489Cn7KeTmejSxaWsws' LIMIT 1
Landlord Load (0.6ms) SELECT "landlords".* FROM "landlords" WHERE "landlords"."authentication_token" = '6489Cn7KeTmejSxaWsws' LIMIT 1
Role Load (0.5ms) SELECT "roles".* FROM "roles" WHERE "roles"."name" = 'admin' LIMIT 1
Role Exists (0.7ms) SELECT 1 AS one FROM "roles" INNER JOIN "landlords_roles" ON "roles"."id" = "landlords_roles"."role_id" WHERE "landlords_roles"."landlord_id" = 5867 AND "roles"."id" = 1 LIMIT 1
Completed 500 Internal Server Error in 275ms
NoMethodError (undefined method `split' for nil:NilClass):
app/controllers/listings_controller.rb:8:in `create'
Rendered /Users/rigelstpierre/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.9ms)
Rendered /Users/rigelstpierre/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.1ms)
Rendered /Users/rigelstpierre/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (16.9ms)
Use:
params[:listing][:images]
And it's already an Array, no need to split. You can directly do: params[:listing][:images].each.
params[:images] is nil, you're after params[:listing][:images] I assume.
Your params are structured like: "listing"=>{"images"=>["70621", "70622", "70620"]}.
Plus, you needn't split it, it's an array. Access each item of the array with: params[:listing][:images].each do |i|

Rails Completed 406 Not Acceptable in 64ms

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/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.

Resources