ruby-on-railsrubykeyword

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]

Solution

  • 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.