简体   繁体   中英

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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