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:
But your post parameters are:
params[:event] is empty.