Rails 3.2更新复选框中未应用的布尔值

时间:2012-12-11 02:00:06

标签: ruby-on-rails ruby postgresql activerecord ruby-on-rails-3.2

我有一个问题,而我正在尝试使用基于复选框的true或false来更新数据库中的布尔字段。

我正在使用rails 3.2.2,使用RVM我尝试使用3.2.9,结果相同。这是在开发中,我正在运行postgresql作为我的数据库。

我正在开发中运行,所以在我到达稍后的测试日期之前,我没有使用attr_accessible。我在这里看到的所有类似问题似乎与忘记将布尔字段添加到attr_accessible有关,所以让每个人都知道这不是问题。

从功能角度来看,这部分应用程序基本上是用户可以安排的事件(或任务)列表。有一个索引页面将提取当天所有任务的列表。然后,用户可以单击任务旁边的复选框,它将通过ajax调用使用相关的布尔字段将任务更新为已完成。

以下是索引视图的相关部分:

=render :partial => "mktg/home/tasks"

这是部分_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

事件控制器的更新方法基本上是搭建的:

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

这是update.js.coffee:

jQuery ->
  $('#mktg_home_tasks').empty()
  $('#mktg_home_past_due').empty()

  $('#mktg_home_tasks').append("<%= escape_javascript(render 'mktg/home/tasks')%>")

我运行了以下资产,通过jquery触发表单提交复选框:

编辑:从我的帖子下面的第一条评论下面有不正确的缩进。

jQuery ->
  $(":checkbox").live "change", ->
    $(this).parents("form:first").submit()

当我按原样运行应用程序时,没有值被提交到数据库。控制台转储如下所示:

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)

所以我尝试将以下的before_save方法添加到Event模型中:

def set_completed_value
  if self.completed == 0
    self.completed = false
  else
    self.completed = true
  end
end

通过添加,我可以选择事件完成,并通过更新数据库字段为true,这是控制台输出:

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)

但是当我取消选中复选框以将已完成的事件标记为未完成时,数据库不会更新。这是控制台输出:

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)

我从来没有像这样添加一个before_save方法来获取一个复选框来更新数据库中的布尔值。所以我一定做错了。我知道我很亲密。如果有人有帮助或建议,我会非常感激。

谢谢...

编辑:尝试John Naegle给出的答案

我改变了

= form_for t, :index => t.id, :remote => true do |task|

= form_for t, :index => t.id, :as => :event, :remote => true do |task|

我得到以下控制台输出

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'

我想要更改的记录ID是133(这是上面的'未知属性')。

以下是包含行号的Update方法

#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

我应该尝试的任何其他内容,或者您​​在我的代码中看到任何错误。再次感谢。

编辑:使用John Naegle在下面的回答我现在有了这个工作

= form_for t, :index => t.id, :as => :event, :remote => true do |task|

以上导致以下控制台输出和后续错误:

"event"=>{"133"=>{"completed"=>"1"}}, "id"=>"133"}

通过删除:index记录ID停止被错误地传递。一旦我改为:

= form_for t, :as => :event, :remote => true do |task|

一切都开始正常运作了。谢谢您的帮助!我不知道如果不是这个网站和帮助的每个人,我有时会“不安”。我爱这个地方。

1 个答案:

答案 0 :(得分:3)

在我看来,你的控制器就是这样做的:

@event.update_attributes(params[:event])

但你的帖子参数是:

"utf8"=>"",
 "authenticity_token"=>"5ahLYhEjY2QEh6G0XOrathPXMSMaiDoCAgABpDbCZck=",
 "mktg_event"=>{"133"=>{"completed"=>"0"}},
 "id"=>"133"}

params [:event]为空。