我看过很多关于 ActionWebService 和 XMLRPC 的例子,但它们已经有 3 年历史了,据我所知,ActiveResource 应该会取代 ActionWebService。
我熟悉 ActiveResource 如何使用 XML 与其他网站“对话”并使用模型信息,但 XML-RPC 是一种完全不同的类型,您可以在其中传递要执行的方法的名称请求被移交等。
编辑 - 我知道 ActiveResource 应该如何工作 - 但我有一个客户端应用程序需要将 XML-RPC 与定义的 API (MetaWeblogAPI) 结合使用,我别无选择,只能实现它 - 束手无策。
所以 - 特别是:我一直在尝试查找一些文档或文章,了解如何使用 ActiveResource 通过 Rails 实现 XML-RPC。也许它不能——我想我也想知道。我只是错过了“小飞跃”——“你如何将请求传递给方法”部分,我从 XML-RPC 请求中提取方法名称并将其传递给方法。我知道我想多了。忍不住 - 我是一个 .NET 人 :)。
我已经尝试“使用有效的方法”——这意味着我已经尝试实现 ActionWebService 但它似乎不能很好地与 Rails 2.3.5(这是我安装的)配合使用,因为我不断得到指向已安装的 ActionWebService 的“未知常量”错误(这让我相信 Rails 2.x 不喜欢它)。
我有点笨,所以要温和 :) - 我确信这可能比我想象的要容易得多。
最佳答案
这比您想象的要容易得多。您不需要在 Rails 中处理 XMLRPC。您可以让您的 Rails 应用程序在请求时提供 XML,并且您可以通过简单地将 .xml 附加到任何 URL 来请求 XML,只要您告诉您的操作如何处理 .xml 请求。这是一个示例操作:
def show
@post = Post.find(:all, :conditions => { :id => params[:id] }
respond_to do |format|
format.html do
# this is the default, this will be executed when requesting http://site.com/posts/1
end
format.xml do
# this will be rendered when requesting http://site.com/posts/1.xml
render :xml => @post
end
end
end
这样,就不需要花哨的 XMLRPC 调用,只需将 .xml 附加到 URL,Rails 就会知道提供 XML 流服务。
要将其与 ActiveResource 一起使用,您只需执行如下操作
class Resource < ActiveResource::Base
self.site = Settings.activeresource.site # 'http://localhost:3000/
self.user = Settings.activeresource.username # Only needed if there is basic or digest authentication
self.password = Settings.activeresource.password
end
class GenreResource < Resource
self.element_name = 'genre'
end
class ArtistResource < Resource
self.element_name = 'artist'
end
class AlbumResource < Resource
self.element_name = 'album'
end)
class TrackResource < Resource
self.element_name = 'track'
end
class AlbumshareResource < Resource
self.element_name = 'albumshare'
end
然后在使用 Rails 提供的内置 API 的应用程序中,您可以调用 TrackResource.exists?(34)
和 track = TrackResource.new(:name =>“rails 名称”); track.save
等
Here's the documentation on ActiveResource .为了让 ActiveResource 工作,只需确保您的 Rails 应用知道在请求时使用 respond_to
提供 XML。
关于ruby-on-rails - 是否有使用 ActiveResource 和 XMLRPC for Rails 的示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1967194/