0

我想通过 Rails 应用程序中的 PUT 请求更新模型。最好的方法是什么?

基本上我有:

def method
  ...
  @relation = Relation.find(34)
  @relation.name = "new_name"
  @relation.save
end

这给了我 SQLite 中的错误(“无法在事务中启动事务”)。

我猜应该切换到 put/post 来解决问题.. 正确的方法是什么?

4

1 回答 1

2

所以过了一段时间,我实际上找到了解决方案。这是 Resque worker 的代码,它通过 PUT 更新关系模型。使用这种方法,我不会收到 SQLite 繁忙异常错误。

class VideoCollector
  def self.perform(rel_id)
    @relation = Relation.find_by_id(rel_id)
    @url = Rails.application.routes.url_helpers.relation_url(@relation)
    @uri = URI(@url)
    @body ={"collected" => "true"}.to_json
    request = Net::HTTP::Put.new(@uri.path, initheader = {'Content-Type' =>'application/json'})
    request.body = @body
    response = Net::HTTP.new(@uri.host, @uri.port).start {|http| http.request(request) }
  end
end

也许这对某人有用。

于 2012-05-28T16:29:18.827 回答