1

我正在开发一个以无表格容量为主的 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 应用程序!

加雷斯

4

1 回答 1

0
  1. 十六进制后缀是您的实例的对象 IDSystem
  2. 您可以通过实现inspect实例方法来操作控制台上的输出
  3. Rails url 帮助程序使用to_param实例方法来构建这些链接。如果你打算使用你的类作为 ActiveRecord 的替代品,你应该实现这个。

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

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

于 2012-09-07T10:28:46.077 回答