- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
这是一个很长的问题,如果需要一段时间才能理解,我深表歉意。我会尽量保持简单。
我在这里开始有点绝望,但我已经很接近了。我一直在与 Ruby 斗争一段时间,并与 post 和 net::http 进行通信。所以我现在得到的是一个散列被转换成 json。使用来自 net::http 的 post 请求通过网络发送,然后我试图将它变回一个对象。
我探索过的一条路线是事件记录和事件资源,但在双方都遇到一些问题后,我决定定义我自己的模型,然后找出如何转换和发送它。
此模型称为 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
所以:
最佳答案
简短的回答是分解一切。我把我的类(class)变成了一个哈希,然后是 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
客户端模型post方法:
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
关于ruby-on-rails - 在 Rails 中从 REST 获取 Post 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6696457/
sanitize 是什么意思在 Rails 中是什么意思? 我正在阅读 CanCanCan 的文档.它说: When using strong_parameters or Rails 4+, you
在过去的几个月里,我感觉自己对 Ruby on Rails (RoR) 开发的了解达到了极限。我为大/小客户和 friend /爱好项目开发了大大小小的应用程序。我知道如何开发这些应用程序,但开始感觉
我昨天参加了一个关于扩展 Rails 的聚会,其中一个主题是 Hexagonal Rails。然而,我只做了一年的 Rails,对 MVC 结构非常满意(也许太舒服了),所以我不太了解适配器和消息队列
我使用多个 Rails 应用程序,一些在 Rails 3.2/Ruby 2.0 上,一些在 Rails 2.3/Ruby 1.8.7 上。 他们的共同点是,随着他们的成长和添加更多的依赖项/ gem
这个问题在这里已经有了答案: Using Rails-UJS in JS modules (Rails 6 with webpacker) (5 个答案) 关闭 3 年前。 我正在尝试使用 UJS
我正在开发一个当前使用 Rails 1.2 的 Rails 应用程序,所以我现在离最新的稳定版本(Rails 2.3)还有很长的路要走。 我应该如何进行迁移到更新版本的 Rails 的过程? 我应该一
尝试按照 Ryan Bates Backbone.js 教程构建抽奖应用程序,但我已经遇到了第一段代码的问题。在 application.js 的 init 函数中,他初始化了 Raffler 路由的
我正在使用 Rails 3.2 并且我有一个数据库表,我想在其中找到符合以下条件的所有行: a = true and b = true and ( 0 true, :b =>
我有一个用户类和一个联系人,其中联系人是用户的子类。这两个类都存储在用户表中。 我的联系人可能有也可能没有电子邮件地址,而我的用户需要一个电子邮件地址(我的用户模型定义中有 validates_pre
我正在编写一个教程,我在其中演示了一些 rails 命令。在我的机器上 rails和 script/rails两者都同样有效。有“首选”形式吗?两者中哪一个更普遍? 最佳答案 当您运行 rails 时
我正在寻找有关通过我的应用程序前进的最佳方式的建议,这是我首次开始集成Elasticsearch。我是一名初学者,但是热衷于深入研究,以便原谅任何明显的错误! 我遵循了http://www.sitep
我刚刚用 Rails new 启动了一个新的 Rails 应用程序,将默认数据库设置更改为 PostgresSQL。我用 bin/rails s 启动服务器,结果很奇怪 2016-04-21 05:0
我收到一个参数并希望它是这样的字符串: "abc,efg" 或者像这样的数组 ["abc","efg"] 在第一种情况下,我想将它转换成一个数组,什么是好的方法? 这是我的想法 if params[:
我刚刚用 Rails new 启动了一个新的 Rails 应用程序,将默认数据库设置更改为 PostgresSQL。我用 bin/rails s 启动服务器,结果很奇怪 2016-04-21 05:0
我收到一个参数并希望它是这样的字符串: "abc,efg" 或者像这样的数组 ["abc","efg"] 在第一种情况下,我想将它转换成一个数组,什么是好的方法? 这是我的想法 if params[:
我有 Rails 4,这是我的默认版本(我仍然希望它是)。但我不想在我的电脑上添加 rails 3.2。在以下命令中:gem install rails -v 3.2.16 我有这个警告: railt
您好,我想使用 Sheevaplug 构建一个“Rails Brick”来自 Marvell(操作系统是开箱即用的 Ubuntu,但您可以在其上安装其他发行版)。它将成为家庭服务器和静音、低成本(99
我需要能够从 Rails 控制台发送我的 Rails 应用程序的 Postgres 数据库中所有未接受的邀请。 (我有一个名为 Invitations 的表,其中包含一个名为 accepted 的 b
validate :cannot_modify_if_locked, on: :update def cannot_modify_if_locked if self.locked erro
我正在学习教程(学习 Rails 播客),需要更改以下路由语法,以便它与 Rails 3.0 兼容。谁能帮忙? map.view_page ':name', :controller => 'viewe
我是一名优秀的程序员,十分优秀!