2011-10-23 83 views
1

我是新來的rails。用軌道中的關係創建幾個對象

我有一個註冊表單,用戶可以在註冊表單的同時創建一個項目。 該項目應該創建,並且新用戶成爲項目的「管理員」。

我有以下型號:

class Project < A::B 
    has_many :roles 
    has_many :users, :through => :roles 
    has_one :admin, :through => :roles, :conditions => "role.name = 'admin'" 
    has_many :members, :through => :roles, :conditions => "role.name = 'member'" 
end 


class User < A::B 
    has_many :roles 
    has_many :projects, :through => :roles 
end 

class Role < A::B 
    belongs_to :projects 
    belongs_to :users 
end 

我在尋找遠去的視圖中創建封裝在模型層的關係設置,並且可以很容易地創建表單,顯示錯誤等,關係中的所有對象。

希望我很清楚,新手到鐵軌。謝謝

回答

1

首先,爲了更容易地創建表格,我建議您使用像formtasticsimple_form這樣的寶石。其次,爲了使嵌套模型更容易,我建議使用像cocoon這樣的寶石,它可以與formtastic或simple_form完美搭配。 Cocoon可以輕鬆創建動態嵌套模型表單。

希望這會有所幫助。

2

你也可以看看由Ryan貝茨截屏: