我想从我的另一个站点解析一些项目。当我添加一个字符串
t.body["Some text"] = "Other text"
替换正文中的一些文本,出现错误:
IndexError in sync itemsController#syncitem
string not matched
lib/sync_items.rb
require 'net/http'
require 'json'
require 'uri'
module ActiveSupport
module JSON
def self.decode(json)
::JSON.parse(json)
end
end
end
module SyncItem
def self.run
uri = URI("http://example.com/api/v1/pages")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
parsed_response = JSON.parse(response.body)
parsed_response.each do |item|
t = Page.new(:title => item["title"], :body => item["body"], :format_type => item["format_type"])
t.body["Some text"] = "Other text"
t.save
end
end
end
我究竟做错了什么?