简体   繁体   English

Rails 4-多态关联-嵌套属性未以形式显示

[英]Rails 4 - Polymorphic Association - Nested Attributes not showing in form

I am building a login system that has two users, buyer and seller, in Rails 4.0.4. 我正在Rails 4.0.4中构建一个具有两个用户(买方和卖方)的登录系统。

For auth I am using the Devise gem: https://github.com/plataformatec/devise 对于身份验证,我正在使用Devise gem: https : //github.com/plataformatec/devise

To create a new buyer I use the route buyer/new. 要创建新买家,请使用路线买家/新买家。 However, the fields for the user do not show in the view. 但是,该用户的字段不会显示在视图中。 I also using debug to show @buyer.user in the view and it has been created. 我还使用debug在视图中显示@ buyer.user ,它已创建。 But when I call f.fields_for @buyer.user do |u| 但是,当我调用f.fields_for @ buyer.user时,| u | the loop is never entered. 循环永远不会进入。

Any ideas of why this is? 为什么会有这个想法? Also, the polymorphic associations seem to be working in the rails console. 而且,多态关联似乎在rails控制台中起作用。

Buyer Controller: 买方总监:

  # GET /buyers/new
  def new
    @buyer = Buyer.new
    @buyer.build_user
  end

Buyer Model 买方模型

class Buyer < ActiveRecord::Base
  has_one :user, as: :role
  accepts_nested_attributes_for :user
end

Buyer/new View 买方/新视图

<%= form_for(@buyer) do |f| %>
  ....
  <div class="field">
    <%= debug(@buyer.user)  %>
    <% f.fields_for @buyer.user do |u| %>
      <%= u.text_field :email %>
    <% end %>
  </div>

User Model 用户模型

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

  belongs_to :role, polymorphic: true

Shouldn't you have an = on the fields_for? 您不应该在fields_for上有一个=吗? http://rubydoc.info/docs/rails/ActionView/Helpers/FormHelper:fields_for http://rubydoc.info/docs/rails/ActionView/Helpers/FormHelper:fields_for

EG 例如

<%= f.fields_for @buyer.user do |u| %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM