Netbeans 中的 hpricot

发布于 2024-08-11 22:48:26 字数 534 浏览 10 评论 0原文

我正在尝试在 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#create

No such file or directory - File not
found - www.wired.com

RAILS_ROOT: H:\Documents and
Settings/owner/My
Documents/NetBeansProjects/RailsApplication5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失而复得 2024-08-18 22:48:26

让 Rails 了解并查看 Hpricot 并不难。

  1. 安装 Hpricot:jruby -S gem install hpricot
  2. 在 Rails 应用中,找到 config/environment.rb 文件
  3. 在文件中找到以 config.gem 开头的行并添加

    config.gem "hpricot", :source =>; "http://code.whytheluckystiff.net"

  4. 现在您将能够像在正常 (J)Ruby 代码中一样直接从控制器使用 Hpricot。我强烈建议不要将任何类型的业务逻辑放入您的视图中,只添加最低限度的条件,以保持理智,并保持事物的直接性、可读性和可维护性。或者,如果您遵循“瘦控制器、简单视图、胖模型”范例,则可以重构代码并将 Hpricot 调用直接放入可从视图访问的模型类内的方法中。

下面是一些代码示例。

示例控制器 RAILS_ROOT/app/controllers/example_controller.rb

class ExampleController < ApplicationController
  def index
    @doc = Hpricot(open("http://www.wired.com"))
    # here come some very serious calculations, queries etc.
  end
end

使用控制器的示例视图 RAILS_ROOT/app/views/example/index.html.erb

<pre>
  <%= @doc ? @doc.to_s : "There is no content at the site" %>
  <!-- blablabla -->
</pre>

正如我所提到的,您也许可以将 Hpricot(open(...)) 调用推回到模型,但首先像这样尝试一下。如果有效,请重构:)

Getting Rails to understand and see Hpricot is not very hard to do.

  1. Install Hpricot: jruby -S gem install hpricot.
  2. In your Rails app, find the config/environment.rb file
  3. Find the lines which start with config.gem in the file and add

    config.gem "hpricot", :source => "http://code.whytheluckystiff.net"

  4. 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:

class ExampleController < ApplicationController
  def index
    @doc = Hpricot(open("http://www.wired.com"))
    # here come some very serious calculations, queries etc.
  end
end

Example view RAILS_ROOT/app/views/example/index.html.erb using the controller:

<pre>
  <%= @doc ? @doc.to_s : "There is no content at the site" %>
  <!-- blablabla -->
</pre>

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文