Netbeans 中的 hpricot
我正在尝试在 JRuby 中使用 hpricot。 我的问题如下。如果我有这个代码:
#!ruby
require 'hpricot'
require 'open-uri'
# load the RedHanded home page
doc = Hpricot(open("http://redhanded.hobix.com/index.html"))
我应该把它放在哪里? 进入我的控制器?因为那里不接受它。 如果我应该将它放入我的模型中。从我的角度来看,我该如何称呼它?
谢谢,
当我尝试使用wired.com网站时,它给了我这个错误
Errno::ENOENT 中 产品控制器#create
没有这样的文件或目录 - 文件不是 找到 - www.wired.com
RAILS_ROOT:H:\文档和 设置/所有者/我的 文档/NetBeansProjects/RailsApplication5
I am trying to use hpricot in JRuby.
My problem is the following. If I have this code:
#!ruby
require 'hpricot'
require 'open-uri'
# load the RedHanded home page
doc = Hpricot(open("http://redhanded.hobix.com/index.html"))
where do I put it?
Into my controller? Because its not accepting it there.
And if I'm supposed to put it in my model. How can I call it frm my view?
Thank you
it gives me this error when trying the wired.com website
Errno::ENOENT in
ProductsController#createNo such file or directory - File not
found - www.wired.comRAILS_ROOT: H:\Documents and
Settings/owner/My
Documents/NetBeansProjects/RailsApplication5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让 Rails 了解并查看 Hpricot 并不难。
jruby -S gem install hpricot
。config/environment.rb
文件在文件中找到以
config.gem
开头的行并添加config.gem "hpricot", :source =>; "http://code.whytheluckystiff.net"
现在您将能够像在正常 (J)Ruby 代码中一样直接从控制器使用 Hpricot。我强烈建议不要将任何类型的业务逻辑放入您的视图中,只添加最低限度的条件,以保持理智,并保持事物的直接性、可读性和可维护性。或者,如果您遵循“瘦控制器、简单视图、胖模型”范例,则可以重构代码并将 Hpricot 调用直接放入可从视图访问的模型类内的方法中。
下面是一些代码示例。
示例控制器
RAILS_ROOT/app/controllers/example_controller.rb
:使用控制器的示例视图
RAILS_ROOT/app/views/example/index.html.erb
:正如我所提到的,您也许可以将
Hpricot(open(...))
调用推回到模型,但首先像这样尝试一下。如果有效,请重构:)Getting Rails to understand and see Hpricot is not very hard to do.
jruby -S gem install hpricot
.config/environment.rb
fileFind the lines which start with
config.gem
in the file and addconfig.gem "hpricot", :source => "http://code.whytheluckystiff.net"
Now you'll be able to use Hpricot directly from a controller as in normal (J)Ruby code. I strongly advise not to put any kind of business logic into your views and only minimal conditionals for sanity and in order to keep things straight, readable and maintainable. Or if you follow the "skinny controllers, simple views, fat models" paradigm, you may be able to refactor the code and put the Hpricot calls directly into a method inside your model class that is accessible from the view.
Some code examples are below.
Example controller
RAILS_ROOT/app/controllers/example_controller.rb
:Example view
RAILS_ROOT/app/views/example/index.html.erb
using the controller:As I've mentioned, you might be able to push the
Hpricot(open(...))
call back to the model, but first give it a try like this. If it's working, refactor :)