2012-05-26 56 views
0

我想通過Rails應用程序中的PUT請求更新模型。什麼是最好的方式來做到這一點?如何從Rails模型向控制器發出PUT請求?

基本上我有:

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

這讓我在SQLite的錯誤(「不能在事務內啓動一個事務」)。

切換到put/post我應該保存問題..什麼是正確的方法來做到這一點?

+0

這種方法在模型中嗎?我沒有看到任何嘗試提出任何請求。 –

+0

這是一個後臺工作者的方法。 – Stpn

+0

我仍然沒有看到這與get/put/post請求有關。 –

回答

2

所以過了一段時間,我真的找到了解決方案。這是Resque worker的代碼,它通過PUT更新Relation模型。使用這種方法我不會得到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 

也許這對別人有用。

相關問題