ActiveResource客户端的行为不符合预期

时间:2009-12-16 15:17:11

标签: ruby-on-rails activeresource

我有这段代码:

require 'rubygems'
require 'activeresource'

ActiveResource::Base.logger = Logger.new("#{File.dirname(__FILE__)}/exercises.log")

class Exercise < ActiveResource::Base
  self.site = "http://localhost"
  exercises = Exercise.find(:all)

  ex = Exercise.find(741)
  ex.name += "_TEST"
  ex.save
end

生成的ex.save网址为

POST http://localhost/exercises.xml

结果是创建了新记录而不是更新现有记录......

我原本希望网址是

PUT http://localhost/exercises/741.xml

当然我希望现有的记录能够更新。

任何想法在哪里看?

由于

1 个答案:

答案 0 :(得分:1)

移动此块:

  exercises = Exercise.find(:all)

  ex = Exercise.find(741)
  ex.name += "_TEST"
  ex.save

类定义的外部。