基于 ActiveModel 的类不会创建与 ActiveRecord 等效项相同的结果
我正在开发一个主要是无表容量的 Rails 3 应用程序。我使用 savon_model 和 ActiveModel 生成与 ActiveRecord 等效项类似的行为。下面是我的代码:
class TestClass
include Savon::Model
include ActiveModel::Validations
# Configuration
endpoint "http://localhost:8080/app/TestService"
namespace "http://wsns.test.com/"
actions :getObjectById, :getAllObjects
attr_accessor :id, :name
def initialize(hash)
@id = hash[:id]
@name = hash[:name]
end
client do
http.headers["Pragma"] = "no-cache"
end
def self.all
h = getAllObjects(nil).to_array
return convert_array_hash_to_obj(h, :get_all_objects_response)
end
def self.find(id)
h = getObjectById(:arg0 => id).to_hash
return convert_hash_to_obj(h, :get_object_by_id_response)
end
private
def self.convert_array_hash_to_obj(arrayhash, returnlabel)
results = Array.new
arrayhash.each do |hash|
results << convert_hash_to_obj(hash, returnlabel)
end
return results
end
def self.convert_hash_to_obj(hash, returnlabel)
return TestClass.new(hash[returnlabel][:return])
end
end
好的,所以一切都按预期进行;值从 Web 服务提取到页面上。不幸的是,当我查看客户端生成的 html 时,出现了一些问题。显示链接如下所示:
/testclasses/%23%3CTestClass:0xa814cb4%3E
而不是...
/testclasses/1
因此,我将对象(哈希?)打印到控制台以比较输出。
[#<System:0xa814cb4 @id="1", @name="CIS">]
而不是我认为应该是的......
[#<System id="1", name="CIS">]
我有三个问题:
1:打印出来我的类名的十六进制后缀是什么
2:如何修改我的类以匹配打印到控制台时所需的输出?
3:为什么前端链接(显示、编辑、删除)损坏了?有简单的修复方法吗?
非常感谢您的宝贵时间,并对垃圾代码/愚蠢的问题表示歉意。这是我的第一个 Ruby 或 Rails 应用程序!
加雷思
I am developing a Rails 3 app in a largely tabless capacity. I am using savon_model and ActiveModel to generate similar behaviour to ActiveRecord equivalents. Below is my code:
class TestClass
include Savon::Model
include ActiveModel::Validations
# Configuration
endpoint "http://localhost:8080/app/TestService"
namespace "http://wsns.test.com/"
actions :getObjectById, :getAllObjects
attr_accessor :id, :name
def initialize(hash)
@id = hash[:id]
@name = hash[:name]
end
client do
http.headers["Pragma"] = "no-cache"
end
def self.all
h = getAllObjects(nil).to_array
return convert_array_hash_to_obj(h, :get_all_objects_response)
end
def self.find(id)
h = getObjectById(:arg0 => id).to_hash
return convert_hash_to_obj(h, :get_object_by_id_response)
end
private
def self.convert_array_hash_to_obj(arrayhash, returnlabel)
results = Array.new
arrayhash.each do |hash|
results << convert_hash_to_obj(hash, returnlabel)
end
return results
end
def self.convert_hash_to_obj(hash, returnlabel)
return TestClass.new(hash[returnlabel][:return])
end
end
OK, so everything works as expected; values are pulled from the web service and onto the page. Unfortunately, when I look at the html produced at the client side there are some issues. The Show links are along the following lines:
/testclasses/%23%3CTestClass:0xa814cb4%3E
instead of...
/testclasses/1
So, I did a print of the object (hash?) to the console to compare the outputs.
[#<System:0xa814cb4 @id="1", @name="CIS">]
instead of what I believe it should be...
[#<System id="1", name="CIS">]
I have three questions:
1: What is the hex suffix on my class name when it is printed out
2: How can I modify my class to match the desired output when printed to the console?
3: Why are the frontend links (Show, Edit, Delete) broken and is there an easy fix?
Thanks so much for your time and apologies for rubbish code / stupid questions. This is my first Ruby or Rails app!
Gareth
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
System
实例的对象 IDinspect
实例方法来操作控制台上的输出to_param
实例方法来构建这些链接。如果您打算使用您的类作为 ActiveRecord 的替代品,您应该实现这个。一般来说,如果您想将所有 Rails 好东西与模型类的自己实现一起使用,则应使用 ActiveModel:Lint::Test 来验证 ActiveModel API 的哪些部分按预期工作。
更多信息可以在这里找到: http://api.rubyonrails.org/classes/ ActiveModel/Lint/Tests.html
System
inspect
instance methodto_param
instance method to build these links. You should implement this if you are going to use your class as an ActiveRecord substitute.Generally speaking, if you want to use all the Rails goodies with an own implementation of a model class, you should use
ActiveModel:Lint::Test
to verify which parts of the ActiveModel APIs are working as expected.More information can be found here: http://api.rubyonrails.org/classes/ActiveModel/Lint/Tests.html