1

我有一个问题,而我正在尝试根据复选框使用 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 方法添加到事件模型中:

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(这是上面的“未知属性”)。

这是带有行号的更新方法

#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|

一切都开始正常工作。谢谢您的帮助!我不知道如果不是因为这个网站和每个提供帮助的人,我有时会如何“摆脱困境”。我喜欢这个地方。

4

1 回答 1

3

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

@event.update_attributes(params[:event])

但是您的帖子参数是:

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

params[:event] 为空。

于 2012-12-11T04:01:26.740 回答