开发者

Nested Select in Rails

开发者 https://www.devze.com 2022-12-31 15:04 出处:网络
I am working on a Rails application which uses categories for items. My category开发者_开发知识库 model is self-joined so that categories can be nested:

I am working on a Rails application which uses categories for items.

My category开发者_开发知识库 model is self-joined so that categories can be nested:

class Category < ActiveRecord::Base
  has_many :items

  # Self Join (categories can have subcategories)
  has_many   :subcategories, :class_name => "Category", :foreign_key => "parent_id"
  belongs_to :parent,        :class_name => "Category"
  ...
end

I have a form which allows a user to create an item which currently lists all categories in a select, but they are all listed together:

<%= f.label :category_id %>
<%= select :item, :category_id, Category.all.collect {|c| [ c.title, c.id ]} %>

So the select looks something like this:

Category1
Category2
Category3BelongsTo2
Category4BelongsTo1

But what I want is:

Category1
  - Category4BelongsTo1
Category2
  - Category3BelongsTo2

Is there a helper for this (which would be awesome!)? If not, how could I accomplish this?

Thanks!


You might want to look at acts_as_nested_set, or awesome_nested_set


awesome_nested_set made this a piece of cake.

After installing, I added lft and rgt to the categories table and removed the self join. Then rebuilt the categories table using Category.rebuild!. Then the select can be easily populated like this:

<%= f.select :parent_id, nested_set_options(Category, @category) {|c| "#{'-' * c.level} #{c.title}" } %>


Maybe grouped_options_for_select would help you

0

精彩评论

暂无评论...
验证码 换一张
取 消