这比你想象的要容易得多。您不需要在 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
然后在使用内置 API rails 提供的应用程序中,您可以进行诸如此类的TrackResource.exists?(34)
调用track = TrackResource.new(:name => "Track Name"); track.save
。
这是有关 ActiveResource 的文档。为了使 ActiveResource 工作,只需确保您的 Rails 应用程序知道在请求时服务 XML,使用respond_to
.