Created
November 22, 2010 20:48
-
-
Save zporter/710649 to your computer and use it in GitHub Desktop.
options_for_select method to accommodate sub-categories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Category < ActiveRecord::Base | |
has_many :categories, :order => 'order_by asc' | |
belongs_to :category | |
scope :order, order("order_by asc") | |
scope :main, where(:category_id => nil) | |
def options_for_select( selected = nil ) | |
selected_attribute = ' selected="selected"' if self == selected | |
options_output = "<option value='#{self.id}'#{selected_attribute}>#{self.title}</option>" | |
self.categories.each do |category| | |
options_output += category.options_for_select( selected ) | |
end | |
options_output.html_safe | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<select> | |
<% Category.main.order.each do |category| %> | |
<%= category.options_for_select Category.find(2) %> | |
<% end %> | |
</select> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment