[英]Rails 4 - Polymorphic Association - Nested Attributes not showing in form
我正在Rails 4.0.4中構建一個具有兩個用戶(買方和賣方)的登錄系統。
對於身份驗證,我正在使用Devise gem: https : //github.com/plataformatec/devise
要創建新買家,請使用路線買家/新買家。 但是,該用戶的字段不會顯示在視圖中。 我還使用debug在視圖中顯示@ buyer.user ,它已創建。 但是,當我調用f.fields_for @ buyer.user時,| u | 循環永遠不會進入。
為什么會有這個想法? 而且,多態關聯似乎在rails控制台中起作用。
買方總監:
# GET /buyers/new
def new
@buyer = Buyer.new
@buyer.build_user
end
買方模型
class Buyer < ActiveRecord::Base
has_one :user, as: :role
accepts_nested_attributes_for :user
end
買方/新視圖
<%= form_for(@buyer) do |f| %>
....
<div class="field">
<%= debug(@buyer.user) %>
<% f.fields_for @buyer.user do |u| %>
<%= u.text_field :email %>
<% end %>
</div>
用戶模型
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :role, polymorphic: true
您不應該在fields_for上有一個=嗎? http://rubydoc.info/docs/rails/ActionView/Helpers/FormHelper:fields_for
例如
<%= f.fields_for @buyer.user do |u| %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.