简体   繁体   English

Ruby on Rails每个do方法

[英]Ruby on Rails Each do method

I'm trying to do my keyword metatags. 我正在尝试做我的关键字元标记。 I want to make the following as keywords. 我想将以下内容作为关键字。

- @document.category.ancestors.each do |category|
= category.description  "

The above works fine on its own in the view, but how can I maybe move that into a helper so I can use it as a method so that I can insert it into the array below? 上面的代码在视图中本身可以很好地工作,但是如何将其移动到帮助器中,以便可以将其用作方法,以便将其插入下面的数组中? Or is there another way I can do this? 还是有其他方法可以做到这一点?

:keywords => [category.descriptions go here]

Create an instance method under your Document model that returns an array of the category descriptions: 在您的Document模型下创建一个实例方法,该方法返回类别描述的数组:

def category_descriptions
  self.ancestors.collect { |a| a.description }
end

Then wherever you have a document object just call .category_descriptions on it and it will return you the array of descriptions. 然后,无论您有何文档对象,都可以在其上调用.category_descriptions,它将返回描述数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM