渲染JSON / XML非ActiveRecord对象[回报率] [英] Render non-activerecord objects in JSON/XML [RoR]
问题描述
我正在测试一个小型的whois API使用红宝石宝石域名注册,并且由于域名注册的反应是相当有趣的,有时我被要求不要使用ActiveRecord用于保存响应格式。
I am testing a small whois API using the ruby gem whois, and due to the format of whois responses being quite funny sometimes i was asked not to use ActiveRecord for saving the responses.
简单地说,这里的工作原理是:
Simply put, here's how it works :
- 用户从一个视图(动作'查找'=创建的请求)输入表单中的域名
- 控制器捕捉参数然后instanciating请求对象[含域名注册响应]发送到模型(非ActiveRecord的)
- 模型使用whois的宝石,并发送回控制器收到WHOIS响应
- 控制器发送不同格式的响应(HTML / JSON / XML),但只有HTML获取对象。
下面是code对我的控制器请求动作查找:
Here is the code for the action "lookup" of my controller "requests" :
def lookup
domain = params[:domain]
@request = Request.new.search(domain)
respond_to do |format|
if @request != nil
format.html
format.json { render :json => @request}
format.xml {render :xml => @request}
else
format.html { render :new }
format.json { render json: @request.errors, status: :unprocessable_entity }
end
end
显然,我有一个困难时期,因为我没有使用ActiveRecord的,而且由于回报率是在等待着他不断随地吐痰nilClass例外。
Obviously, i'm having a hard time because i'm not using ActiveRecord, and since RoR is expecting one he keeps spitting nilClass exceptions.
所以,当我去本地主机:8080 /请求/查找
一切都显示正常,并@request包含了所有我想要的数据。
但阉本地主机:8080 /请求/ lookup.json
或本地主机:8080 /请求/ lookup.xml
没有显示如果我尝试在建设者发出指示(在Jbuilder / XMLBuilder),它抛出一个nilClass异常,证明了可变范围并不全球...
So when i go on localhost:8080/requests/lookup
everything is displayed fine, and @request contains all the data i want.
But wether localhost:8080/requests/lookup.json
or localhost:8080/requests/lookup.xml
nothing shows up and if I try giving instructions in the builders (Jbuilder/XMLBuilder) it throws a nilClass exception, proving that the variable scope isn't so global...
不,我不认为投入变量会是个好主意:我只用它为单个查询
And no, i don't think putting in the variable session is a good idea : I will only be using it for a single query.
如果需要的话,我会很乐意提供更多我的code。如果它可以帮助你理解我的问题。我知道AR是要走的路,但尽管如此我很好奇,知道怎么去解决它像这样的情况。
If needed, i'll be happy to provide more of my code if it may help you understand my problem. I know AR is the way to go, but nonetheless i am curious as to know how to get around it for situations such as this one.
感谢您!
推荐答案
您可以使用加载ActiveModel,即使你不使用的ActiveRecord。轨道4使它很容易。您还可以添加加载ActiveModel ::系列化它允许您使用<序列化对象code> .to_json 和 .to_xml
You can use ActiveModel even though you are not using ActiveRecord. Rails 4 made it really easy. You can also add ActiveModel::Serialization which allows you to serialize the objects with .to_json
and .to_xml
class WhoisLookup
include ActiveModel::Model
include ActiveModel::Serializers::JSON
include ActiveModel::Serializers::Xml
attr_accessor :search_term # ...
# you can use all the ActiveModel goodies
validates :search_term, presence: true, length: {in:2..255}
end
加载ActiveModel ::序列化将允许您使用:
ActiveModel::Serialization will allow you to use:
format.json { render :json => @result } # calls @result.to_json
PS。鸵鸟政策使用 @request
的变量命名(@result也许?)you're绑定到 ActionDispatch遇到的问题和困惑::请求
。
PS. don´t use @request
for variable naming (@result maybe?) you´re bound to run into issues and confusion with ActionDispatch::Request
.
这篇关于渲染JSON / XML非ActiveRecord对象[回报率]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!