2010-06-11 79 views
2

我有一篇文章模型和一個類別模型。類別充當樹。Formtastic +嵌套類別

構建選擇列表的最佳方法是什麼?允許管理員從選擇列表中選擇一個類別,以便稍後將其與文章關聯起來?

semantic_form_for(@article) do |f| 
    f.input :title, :as => :string 
    f.input :content, :as => :text 
    f.input :category, :collection => #what should go here ? 
end 
+0

8年後,你有沒有想出解決辦法? :p – Tashows 2018-01-21 16:42:06

回答

0

試試這個:

f.input :category, :as => :select, :collection => Category.all 

另外,您可以指定它們,因爲這(從文檔):

f.input :category, :as => :select, :collection => { @cat1.name => @cat1id, @cat2.name => @cat2.id } 
f.input :category, :as => :select, :collection => ["cat1", "cat2", "cat3", "cat4", "cat5"]