这是一个很长的问题,所以如果需要一段时间才能掌握,我道歉。我会尽可能地保持简单。
我开始有点绝望,但我很亲密。我一直在与Ruby挣扎一段时间并与post和net :: http进行交流。所以我现在得到的是哈希变成了json。使用来自net :: http的帖子请求发送到电汇,然后我试图将其变回对象。
我探索过的一条路线是活动记录和活动资源但是在双方的一些问题之后决定定义我自己的模型,然后找出如何转换并发送它。
此模型称为customRequest。在其中我有post_me,它将2个类变量转换为哈希,将其转换为json,然后发送它。
class CustomRequest
require 'json'
require 'net/http'
attr_accessor :myString, :myInteger
def initialize(myString,myInteger)
@myString = myString
@myInteger = myInteger
end
def post_me
@myReq = {:myString => @myString, :myInteger =>@myInteger}
myJsonReq = @myReq.to_json
#myJsonReq = JSON.generate(@myReq)
puts myJsonReq
# res = Net::HTTP.post_form(URI.parse('http://127.0.0.1:3008/user_requests/add'),
# myJsonReq)
res = Net::HTTP.post_form(URI.parse('http://127.0.0.1:3008/user_requests/add.json'),
myJsonReq).as_json
end
end
简而言之就是自定义请求。
我关注的是:
如果我在rails控制台中创建类并发送它,我会在终端中得到一个很好的200和一个响应。
(注意:如果你真的想知道场景中发生了什么,只读这个位)
@test.post_me
{"myInteger":300,"myString":"testestest"}
=> {"x-ua-compatible"=>["IE=Edge"], "etag"=>["\"d41d8cd98f00b204e9800998ecf8427e\""], "connection"=>["Keep-Alive"], "content-type"=>["application/json; charset=utf-8"], "date"=>["Thu, 14 Jul 2011 15:54:24 GMT"], "server"=>["WEBrick/1.3.1 (Ruby/1.8.7/2009-06-12)"], "x-runtime"=>["0.022517"], "content-length"=>["0"], "set-cookie"=>["_webApp_session=BAh7BiIPc2Vzc2lvbl9pZCIlZTJmM2IwYmVhZWMwM2E2ZGQzNWEwMDUwNmE2NDhlM2U%3D--5312eec4795d9f0633520c01992422e9c15746e4; path=/; HttpOnly"], "cache-control"=>["max-age=0, private, must-revalidate"]}
>>
如果我不将它转换为Json,那么同样的反应:
{"myInteger":300,"myString":"testestest"}
=> #<Net::HTTPOK 200 OK readbody=true>
如果有人能回答问题1和2我会很高兴
继承服务器端需要接受请求并将其转回Json和对象。我尝试了很多不同的东西,大多数已被注释掉了。
基本上我会在标签中获得200响应,但是当我尝试放入哈希对象时,我什么都没得到
Started POST "/user_requests/add" for 127.0.0.1 at Thu Jul 14 16:56:07 +0100 2011
Processing by UserRequestsController#add_request as
Parameters: {"{\"myInteger\":300,\"myString\":\"testestest\"}"=>""}
Rendered user_requests/add_request.json.erb (0.3ms)
Completed 200 OK in 31ms (Views: 27.4ms | ActiveRecord: 0.0ms)
这是多次将它拆开并将其变回物体的尝试。
def add_request
require 'rubygems'
require 'json'
#@custom = CustomRequest.new(params[:custom])
#@custom = CustomRequest.new(JSON.parse(params[:custom]))
#@custom = CustomRequest.new(params[:custom]).from_json
#@custom = CustomRequest.new(params[:custom].from_json)
@myHash = Hash.new(params[:myHash])
#@myHash = Hash.new(JSON.parse(params[:myHash]))
#@myHash = Hash.new(params[:myHash]).from_json
#@myHash = Hash.new(params[:myHash].from_json)
puts "IT WORKS!"
puts @myHash.to_s
它们几乎都是一样的,除了一个是我试图从发送的哈希重新创建对象而另一个是我试图重新创建hash_to_json到hash_from_json
所以:
答案 0 :(得分:2)
简短的回答是打破一切。我把我的班级改为哈希,然后转到Json,发送。从Json恢复,并重新创建对象。
服务器端的CONTROLLER ::
class UserRequestsController < ApplicationController
# POST /user_requests
# POST /user_requests.xml
def add_request
require 'rubygems'
require 'json'
#@user_request = UserRequest.new(params[:user_request])
#@user_request = UserRequest.new(JSON.parse(params[:user_request]))
#@user_request = UserRequest.new(params[:user_request].from_json)
#puts @user_request
#@custom = CustomRequest.new(params[:custom])
#@custom = CustomRequest.new(JSON.parse(params[:custom]))
#@custom = CustomRequest.new(params[:custom]).from_json
#@custom = CustomRequest.new(params[:custom].from_json)
@myHash = Hash.new()
@myHash = params
#puts (params[:url])
#puts YAML::dump(params)
# puts YAML::dump(params[:url])
#@myHash = Hash.new(JSON.parse(params[:myHash]))
#@myHash = Hash.new(params[:myHash]).from_json
#@myHash = Hash.new(params[:myHash].from_json)
puts "IT WORKS!"
#puts @myHash
#puts YAML::dump(@myHash)
#@myHash.each { |k,v| puts "#{k} => #{v}" }\
#@user_request = CustomRequest.new(@myHash[:url],@myHash[:depth])
#@user_request = CustomRequest.new(@myHash[:url],@myHash[:depth])
#@user_request = CustomRequest.new(Time.now,Time.now,params[:depth],params[:depth],nil)
# Had to manually create the record because it couldnt take it from a single param
@user_request = CustomRequest.create(:created_at =>Time.now,
:updated_at => Time.now,
:depth => @myHash[:depth],#myHash is the params broken into a hash
:url => @myHash[:url],
:status => "Yet to be crawled")
puts "YAML DUMP CUSTOM OBJECT"
puts YAML::dump(@user_request)
respond_to do |format|
if @user_request.save
format.json { render :json => @myHash, :status => :created}
else if @myHash.nil?
format.json { render :json => @user_request.errors, :status => :unprocessable_entity }
#ß format.xml { render :xml => @user_request, :status => :created}#, :location => @user_request }
else
format.json { render :json => @user_request.errors, :status => :unprocessable_entity }
#ß format.xml { render :xml => @user_request, :status => :created}#, :location => @user_request }
end
end
end
客户端的模型发布方法:
def post_me
res = Net::HTTP.post_form(URI.parse('http://127.0.0.1:3008/user_requests/add.json'),
{'url' =>'UserRequest::url'}).as_json