我對用戶註冊使用多態關聯的表單存在問題。我想顯示來自多態關聯的多個模型的字段,但我不能。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 %>
所以在我的瀏覽器只出現「生物」字段,但不出現用戶模型(設計)的擴展字段。
感謝瓦列裏,但它仍然顯示窗體視圖沒有用戶字段。 – 2013-03-17 16:24:10
<%= f.fields_for ... – 2013-03-17 18:23:10
是的,我改變了,但沒有。 – 2013-03-17 18:39:25