Rails 不是 Magic,方法在哪里?

发布于 2024-10-01 16:12:35 字数 640 浏览 1 评论 0原文

我有一段很好的功能性且经过测试的 Ruby 代码,我想将其集成到 Rails 项目中:

require 'open-uri'

def shorten(url)
  open("http://is.gd/api.php?longurl=#{url}").read
rescue
  nil
end

我想通过以下方式从 show.html.erb 调用方法 Short(url):

<% @shorturl = shorten("http://www.google.com/search?q=#{@bands.item.gsub(' ', '+')}+san+francisco") %>

我认为放置该方法 Short ,在模型中可以工作,但会导致 NoMethodError (要求“open-uri”放置在 application_controller.rb 中)

undefined method `shorten' for #<ActionView::Base:0x1037e66a8>

此方法应该位于哪里?我怎样才能让它发挥作用?您推荐哪些书籍或资源可以让 Rails 显得不那么神奇。先感谢您。

I have a nice piece of functional and tested Ruby code that I would like to integrate into a rails project:

require 'open-uri'

def shorten(url)
  open("http://is.gd/api.php?longurl=#{url}").read
rescue
  nil
end

I want to call the method shorten(url) from show.html.erb in the following way:

<% @shorturl = shorten("http://www.google.com/search?q=#{@bands.item.gsub(' ', '+')}+san+francisco") %>

I thought that placing the method shorten, within the model would work but it results in a NoMethodError (the require 'open-uri' is placed in the application_controller.rb)

undefined method `shorten' for #<ActionView::Base:0x1037e66a8>

Where should this method live? How can I get this to work? And what book or resource do you recommend that would make Rails seem less magical. Thank you in advance.

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

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

发布评论

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

评论(3

汐鸠 2024-10-08 16:12:35

我的经验是:如果该方法正在处理数据库数据,最好将其移至模型。即使是处理数据库数据中的数据,如果可能的话,建议将其移至模型中,尽可能拥有“精益控制器”和“精益视图”。

如果该方法一般用于帮助显示某些内容,那么它就属于“帮助者”...例如,h方法就是“帮助查看代码”的经典示例。您可以将此类方法添加到应用程序帮助程序文件中,即 app/helpers/application_helpers.rb

如果它不处理视图,也不处理数据库中的数据,那么您可能会认为它是一个该控制器类的方法,甚至是应用程序控制器的方法(如果它对于您的应用程序非常重要并且应该由所有控制器继承)。

My experience has been: if the method is dealing with database data, best to move it to Model. Even if it is about processing data from database data, it is recommended to move it into Model if possible, to have "lean controller" and "lean view" if possible.

If the method is for generally helping to display something, then it belongs to the "helper"... for example, the h method is a classic example of "helping the View code". You can add such methods to the application helper file, which is app/helpers/application_helpers.rb

If it is not dealing with view and not dealing with data from the database, then you might consider it being a method of that controller class, or even a method of the application controller if it is very central to your application and should be inherited by all controllers.

心碎无痕… 2024-10-08 16:12:35

好吧,如果它是属于模型的方法,那么它要么是类方法,要么是实例方法:

类方法

#some controller
MyModel.method(...)

#MyModel
def self.method(...)
  ...
end

实例方法

#some controller
@myInstance = MyModel.find...(...)
@myInstance.method(...)

#MyModel
def method(...)
  ...
end

如果不将其放入任何模型中,我推荐一个像动静能量说的帮手。

Well, if it's a method that belongs to the model, either it's a class method or an instance method :

Class method

#some controller
MyModel.method(...)

#MyModel
def self.method(...)
  ...
end

Instance method

#some controller
@myInstance = MyModel.find...(...)
@myInstance.method(...)

#MyModel
def method(...)
  ...
end

If it's not to be put in any model, I recommend an helper like 動靜能量 said.

第七度阳光i 2024-10-08 16:12:35

动静能量的回答非常好,所以我补充一点,如果你想学习一些 Rails,请查看这个免费的在线教程,它会引导你逐步构建 Twitter 克隆:http://railstutorial.org/book

我读过的几本书都说先学习 Rails(包括上面的链接),但我认为这是一个可怕的想法,真的非常糟糕的建议。如果您不了解 Ruby,那么您就无法理解什么是 Rails“魔法”以及什么是标准 Ruby。

所以我的建议是先学习Ruby,然后再学习Rails。它看起来不会那么令人沮丧和神奇。以下是我最喜欢的一些有关 Ruby 的书籍:

Programming Ruby 1.9< br>
元编程 Ruby
重构:Ruby 版

我喜欢务实,因为作者的努力得到了更多的钱,而且每个参与的人似乎都非常关心他们产品的质量。

動靜能量's answer is pretty good so I'll just add that if you want to learn some rails check out this free online tutorial that takes you step by step through building a Twitter clone: http://railstutorial.org/book

Several books I've read say to learn Rails first (including the above link) but I think this is a horrible idea and really, really bad advice. If you don't understand Ruby then you can't understand what is Rails "magic" and what's just standard Ruby.

So my advice is to learn Ruby first then learn Rails. It will be MUCH less frustrating and magical seeming. Here are some of my favorite books on Ruby:

Programming Ruby 1.9
Metaprogramming Ruby
Refactoring: Ruby Edition

I love Pragmatic because the authors get a lot more money for their efforts and everyone involved seems to really care about the quality of their products.

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