XML => HTML 与 Hpricot 和 Rails
我从未使用过 Web 服务和 Rails,显然这是我需要学习的东西。 我选择使用 hpricot 因为它看起来很棒。 无论如何,_why 已经很好地在 hpricot 网站 上提供了以下示例:
#!ruby
require 'hpricot'
require 'open-uri'
# load the RedHanded home page
doc = Hpricot(open("http://redhanded.hobix.com/index.html"))
# change the CSS class on links
(doc/"span.entryPermalink").set("class", "newLinks")
# remove the sidebar
(doc/"#sidebar").remove
# print the altered HTML
puts doc
看起来简单、优雅,和简单的豌豆。 在 Ruby 中工作得很好,但我的问题是:如何在 Rails 中分解它?
我尝试将这一切添加到单个控制器中,但想不出在视图中调用它的最佳方法。
因此,如果您从 Web API 解析 XML 文件并使用 Hpricot 将其打印为干净的 HTML,您将如何分解模型、视图和控制器上的活动,以及您将把什么放在哪里?
I have never worked with web services and rails, and obviously this is something I need to learn.
I have chosen to use hpricot because it looks great.
Anyway, _why's been nice enough to provide the following example on the hpricot website:
#!ruby
require 'hpricot'
require 'open-uri'
# load the RedHanded home page
doc = Hpricot(open("http://redhanded.hobix.com/index.html"))
# change the CSS class on links
(doc/"span.entryPermalink").set("class", "newLinks")
# remove the sidebar
(doc/"#sidebar").remove
# print the altered HTML
puts doc
Which looks simple, elegant, and easy peasey.
Works great in Ruby, but my question is: How do I break this up in rails?
I experimented with adding this all to a single controller, but couldn't think of the best way to call it in a view.
So if you were parsing an XML file from a web API and printing it in nice clean HTML with Hpricot, how would you break up the activity over the models, views, and controllers, and what would you put where?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模型,模型,模型,模型,模型。 纤薄的控制器,简单的视图。
RedHandedHomePage 模型在初始化时进行解析,然后在控制器中调用“def render”,将输出设置为实例变量,并将其打印在视图中。
Model, model, model, model, model. Skinny controllers, simple views.
The RedHandedHomePage model does the parsing on initialization, then call 'def render' in the controller, set output to an instance variable, and print that in a view.
我可能会采用 REST 方法,并拥有代表正在使用的 XML 文件中不同实体的资源。 您可以提供具体的 XML 示例吗?
I'd probably go for a REST approach and have resources that represent the different entities within the XML file being consumed. Do you have a specific example of the XML that you can give?