Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8971293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:55:10+00:00 2026-06-15T17:55:10+00:00

I have an issue whereas I am trying to update a boolean field in

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:55:12+00:00Added an answer on June 15, 2026 at 5:55 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Strange issue - I have a live copy of a database class (using PDO)
I have issue with: <form:checkboxes path=roles cssClass=checkbox items=${roleSelections} /> If previous line is used
I have an issue. I am getting data from a MySQL database, and make
I am trying to call a Restful WS from GWT using JSOPRequestBuilder. I have
I have been trying to get right solution / answer for my issue, still
I am trying to learn Backbone.js and have stuck at an issue. model.save is
I'm trying to create a search engine for an inventory based site. The issue
I have a very strange issue here. I am using a present modal view
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
We are new to ROR, We have issue in creating Login/Logout process in ROR

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.