“编辑/更新”操作不会更新记录

时间:2012-05-28 20:48:28

标签: ruby-on-rails

我正在试图弄清楚为什么我的编辑操作不会更新记录。我已多次检查我的控制器编辑和更新操作,但找不到任何错误。我错过了什么?感谢。

goals_controller.rb

def edit
  @goal = Goal.find(params[:id])
end

def update
  @goal = Goal.find(params[:id])
  if @goal.update_attributes(params[:goal])
    flash[:notice] = "Successfully updated goal."
    redirect_to goals_url
  else
    render :action => 'edit'
  end
end

来自控制台的消息:

Started PUT "/goals/11" for 127.0.0.1 at 2012-05-29 04:44:55 +0800
Processing by GoalsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"a2rj6zUT9+ciOcjEH4ESEpMXstoiUcIi48O09Bea2ro=", "goal"=>{"title"=>"My 1st goal dcwecqwe", "description"=>"This is my 1st goal!!!", "start_date"=>"01/05/2012", "due_date"=>"31/05/2012"}, "commit"=>"Update Goal", "id"=>"11"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Goal Load (0.2ms)  SELECT "goals".* FROM "goals" WHERE "goals"."id" = ? LIMIT 1  [["id", "11"]]
  CACHE (0.0ms)  SELECT "goals".* FROM "goals" WHERE "goals"."id" = ? LIMIT 1  [["id", "11"]]
   (0.1ms)  begin transaction
  Update Load (0.2ms)  SELECT "updates".* FROM "updates" WHERE "updates"."goal_id" = 11 LIMIT 1
   (0.1ms)  commit transaction
Redirected to http://localhost:3000/goals
Completed 302 Found in 13ms (ActiveRecord: 0.8ms)

使用控制台

1.9.3p194 :036 >   a = Goal.last
  Goal Load (0.3ms)  SELECT "goals".* FROM "goals" ORDER BY "goals"."id" DESC LIMIT 1
+----+-------------+----------------------+------------+------------+-------+---------+-----------+----------------------+----------------------+
| id | title       | description          | start_date | due_date   | state | user_id | review_id | created_at           | updated_at           |
+----+-------------+----------------------+------------+------------+-------+---------+-----------+----------------------+----------------------+
| 11 | My 1st goal | This is my 1st go... | 2012-05-01 | 2012-05-31 |       | 1       | 3         | 2012-05-28 20:44:... | 2012-05-28 20:44:... |
+----+-------------+----------------------+------------+------------+-------+---------+-----------+----------------------+----------------------+
1 row in set
1.9.3p194 :037 > a.update_attributes(:title => "New title")
   (0.2ms)  begin transaction
  Update Load (0.1ms)  SELECT "updates".* FROM "updates" WHERE "updates"."goal_id" = 11 LIMIT 1
   (0.1ms)  commit transaction
 => true 
1.9.3p194 :038 > a.save
   (0.2ms)  begin transaction
   (0.1ms)  commit transaction
 => true 
1.9.3p194 :039 > Goal.last
  Goal Load (0.5ms)  SELECT "goals".* FROM "goals" ORDER BY "goals"."id" DESC LIMIT 1
+----+-------------+----------------------+------------+------------+-------+---------+-----------+----------------------+----------------------+
| id | title       | description          | start_date | due_date   | state | user_id | review_id | created_at           | updated_at           |
+----+-------------+----------------------+------------+------------+-------+---------+-----------+----------------------+----------------------+
| 11 | My 1st goal | This is my 1st go... | 2012-05-01 | 2012-05-31 |       | 1       | 3         | 2012-05-28 20:44:... | 2012-05-28 20:44:... |
+----+-------------+----------------------+------------+------------+-------+---------+-----------+----------------------+----------------------+
1 row in set
1.9.3p194 :040 > 

0 个答案:

没有答案
相关问题