Ruby on rails 如何从Rails模型向控制器发出PUT请求?
我想通过Rails应用程序中的PUT请求更新模型。最好的方法是什么 基本上,我有:Ruby on rails 如何从Rails模型向控制器发出PUT请求?,ruby-on-rails,ruby,http,sqlite,put,Ruby On Rails,Ruby,Http,Sqlite,Put,我想通过Rails应用程序中的PUT请求更新模型。最好的方法是什么 基本上,我有: def method ... @relation = Relation.find(34) @relation.name = "new_name" @relation.save end 这给了我SQLite中的错误(“无法在事务中启动事务”) 切换到put/post我想应该可以解决这个问题。。什么是正确的方法呢?因此,过了一段时间,我终于找到了解决办法。下面是Resque worker的代码,它通
def method
...
@relation = Relation.find(34)
@relation.name = "new_name"
@relation.save
end
这给了我SQLite中的错误(“无法在事务中启动事务”)
切换到put/post我想应该可以解决这个问题。。什么是正确的方法呢?因此,过了一段时间,我终于找到了解决办法。下面是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
也许这对某些人有用。这种方法在模型中吗?我没有看到任何试图发出任何请求的尝试。这是后台工作程序中的一个方法。我仍然看不到这与get/put/post请求之间的关系。我不使用Relation.save,而是想将“new_name”添加到@Relation url,希望它能帮助我处理事务错误中的事务