2012-09-24 22 views
0

我遇到了我的第一個多對多關係連接表的問題。我是否需要連接表的模型或控制器?

class Category < ActiveRecord::Base 
    has_and_belongs_to_many :users 
end 

class User < ActiveRecord::Base 
    has_and_belongs_to_many :categories 
end 

而且我添加了一個連接表:

create_table "categories_users", :id => false, :force => true do |t| 
    t.integer "category_id", :null => false 
    t.integer "user_id",  :null => false 
end 

有人點我,我會用它來將用戶添加到一個類別形式的例子嗎?我需要一個單獨的寧靜控制器來處理'categories_users'嗎?我有一種衝動,即將方法添加到名爲「添加用戶」的類別控制器中,但我不確定對於其他RESTful控制器而言,這是否明智。

回答

0

我正在使用simple_form,我終於在文檔中找到了該做什麼。因此,使用simple_form時,表單將如下所示:

<%= simple_form_for [:admin, @festival, @category] do |c| %> 
    <%= c.input :name %> 
    <%= c.input :description, input_html: { cols: 100, rows: 3, class: "span6" } %> 
    <%= c.input :takes_submissions %> 
    <%= c.association :users, label_method: :full_name, label: "Choose curators" %> 
    <%= c.button :submit %> 
<% end %> 

無需添加控制器或單獨的窗體。好極了!

相關問題