基于 ActiveModel 的类不会创建与 ActiveRecord 等效项相同的结果

发布于 2024-09-29 14:35:03 字数 1740 浏览 1 评论 0原文

我正在开发一个主要是无表容量的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋千易 2024-10-06 14:35:03
  1. 十六进制后缀是 System 实例的对象 ID
  2. 您可以通过实现 inspect 实例方法来操作控制台上的输出
  3. Rails url 帮助程序使用 to_param 实例方法来构建这些链接。如果您打算使用您的类作为 ActiveRecord 的替代品,您应该实现这个。

一般来说,如果您想将所有 Rails 好东西与模型类的自己实现一起使用,则应使用 ActiveModel:Lint::Test 来验证 ActiveModel API 的哪些部分按预期工作。

更多信息可以在这里找到: http://api.rubyonrails.org/classes/ ActiveModel/Lint/Tests.html

  1. The hex suffix is the object id of your instance of System
  2. You can manipulate the output on the console by implementing an inspect instance method
  3. The Rails url helpers use the to_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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文