2014-01-17 60 views
0

我有以下型號:表單提交嵌套色器件的用戶數據

class User < ActiveRecord::Base 

    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :encryptable 

    belongs_to :club 
end 

class Club < ActiveRecord::Base 
    has_many :users 

    accepts_nested_attributes_for :users 
end 

因爲「accepts_nested_attributes_for」必須繼續下去了「的has_many」的一面,我怎麼構建一個嵌套表單,請接受嵌套,設計,用戶數據?

+0

http://railscasts.com/episodes/196-nested-model-form-part-1 – dbKooper

回答

1

好的。我認爲它應該工作:

控制器:

@users = @ club.users.build

形式:

form_for @club 
    fields_for @users |fr| 
    @users.each do |usr| 
     text_field :email 
     text_field :password 
     text_field :pass_conf... 
      other fields 
    end 
    end 
    submit 
    end 
+0

謝謝,這可以工作,但它不能爲新創建的用戶 – pingu

+0

創建一個會話,你的意思是稍後爲新用戶或稍後創建會話rd被創建? 剛剛創建用戶後,您將不得不創建用戶會話,因爲不涉及設計 devise_way:sign_in_and_redirect @ users.first – dbKooper