2013-03-16 12 views
0

我對用戶註冊使用多態關聯的表單存在問題。我想顯示來自多態關聯的多個模型的字段,但我不能。Form with Rails多態關聯用戶註冊

模型類:

class User < ActiveRecord::Base 
    devise ... 

    belongs_to :userable, :polymorphic => true 
end 

class Advisor < ActiveRecord::Base 
    has_one :user, :as => :userable 
    attr_accessible :extra 
    accepts_nested_attributes_for :user # Idon't know if it is ok 
end 
class AnotherKingOfUser < ActiveRecord::Base 
    #the same.. 
end 

我想爲每個特定用戶類型的控制器,所以:

class AdvisorsController < ApplicationController 
    # GET /advisors/new 
    # GET /advisors/new.json 
    def new 
    # Here is where I'm not sure about what I did. I tried all variants buy any works for me. 
    @advisor = Advisor.new 
    @advisor.build_user 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @advisor } 
    end 
    end 
end 

而我的觀點:

<%= form_for(@advisor) do |f| %> 
    <% if @advisor.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@advisor.errors.count, "error") %> prohibited this advisor from being saved:</h2> 

     <ul> 
     <% @advisor.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

<% #f.fields_for :user do |ff| %> 
<% f.fields_for :user_attributes, @advisor.user||User.new do |ff| %> 

    <div class="field"> 
     <%= ff.label :email %><br /> 
     <%= ff.text_field :email %> 
    </div> 

    <div class="field"> 
     <%= ff.label :password %><br /> 
     <%= ff.text_field :password %> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :bio %><br /> 
    <%= f.text_field :bio %> 
    </div> 


    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

所以在我的瀏覽器只出現「生物」字段,但不出現用戶模型(設計)的擴展字段。

回答

1
class Advisor < ActiveRecord::Base 
    has_one :user, :as => :userable 
    attr_accessible :extra, :user_attributes 
    accepts_nested_attributes_for :user # its ok 
end 

class AdvisorsController < ApplicationController 

    def new 
    @advisor = Advisor.new 
    # @advisor.build_user # unnecessary 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @advisor } 
    end 
    end 
end 

# view 
<%= form_for(@advisor) do |f| %> 
    <%= f.fields_for :user_attributes, @advisor.user||User.new do |ff| %> 
    <div class="field"> 
     <%= ff.label :email %><br /> 
     <%= ff.text_field :email %> 
    </div> 
    <div class="field"> 
     <%= ff.label :password %><br /> 
     <%= ff.text_field :password %> 
    </div> 
    <% end %> 
    ... 
<% end %> 
+0

感謝瓦列裏,但它仍然顯示窗體視圖沒有用戶字段。 – 2013-03-17 16:24:10

+0

<%= f.fields_for ... – 2013-03-17 18:23:10

+0

是的,我改變了,但沒有。 – 2013-03-17 18:39:25

1

當你用這種形式處理關聯時(通常使用accepts_nested_attributes_for,就像你的情況一樣),你需要創建對象以便它們顯示在表單上。

這意味着,在這裏,當軌道建立表單,它看重的是@advisor.user的內容,因爲我想在你的控制器,你不喜歡的東西

@advisor = Advisor.new 

所以,如果你想爲字段裏面是空的用戶出現,你需要build用戶

@advisor = Advisor.new 
@advisor.build_user # this will create a instance of User, but will not persist it 

與該建築物的形式時,Rails會找到用戶,然後顯示所需的字段。

我希望能回答你的問題。

+0

感謝@pjam,有意識,但它會引發以下錯誤:未定義的方法'建設」爲無:NilClass – 2013-03-17 01:52:29

+0

我認爲'@ advisor'要麼'UserA'或'UserB'的實例,你能否在原始文章中添加完整的控制器代碼來處理'@ advisor' instanciation? – pjam 2013-03-17 04:05:00

+0

是的,抱歉讓人混淆。我使用真實的類名編輯了文章,現在我實現了build_user方法,不會拋出任何錯誤,但仍然在窗體視圖中顯示沒有用戶字段。 – 2013-03-17 07:12:39