XML =>带有Hpricot和Rails的HTML [英] XML => HTML with Hpricot and Rails

查看:73
本文介绍了XML =>带有Hpricot和Rails的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从未使用过Web服务和Rails,显然这是我需要学习的东西. 我选择使用hpricot,因为它看起来很棒. 无论如何,_why足以在 hpricot网站上提供以下示例:

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

看起来简单,优雅和轻松. 在Ruby中效果很好,但我的问题是:如何将其拆分?

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.

因此,如果您要从Web API解析XML文件并使用Hpricot将其打印为干净的HTML,那么如何拆分模型,视图和控制器上的活动,又将放置在何处?

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?

推荐答案

模型,模型,模型,模型,模型.瘦控制器,简单视图.

Model, model, model, model, model. Skinny controllers, simple views.

RedHandedHomePage模型在初始化时进行解析,然后在控制器中调用"def render",将输出设置为实例变量,然后在视图中打印出来.

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.

这篇关于XML =>带有Hpricot和Rails的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆