2012-09-09 40 views
3

你好我使用设计注册用户,我想创建一个用户配置文件每次用户注册时,问题是,当我尝试添加满从对色器件的用户注册登记视图轮廓模型的人的名字,它不显示...Rails 3 fields_for has_one不能渲染表单字段设计

这是我的模型:

class User < ActiveRecord::Base 

    # Include default devise modules. Others available are: 
    # :token_authenticatable, :confirmable, 
    # :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me, :profile_attributes 

    has_one :profile, dependent: :destroy 
    accepts_nested_attributes_for :profile 
    # attr_accessible :title, :body 
end 

这是剖面模型

class Profile < ActiveRecord::Base 
    attr_accessible :email, :name, :phone, :code 
    belongs_to :user 

    validates_presence_of :user 
end 

这是修改后的色器件的看法:

<% provide(:title, 'Sign up') %> 
<h2>Sign up</h2> 

<div class="row"> 
    <div class="span6 offset3"> 

    <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> 
     <%= render 'shared/error_messages', object: resource %> 

     <%= f.fields_for :profile do |profile_form| %> 
     <%= profile_form.label :full_name %> 
     <%= profile_form.text_field :name %> 
     <% end %> 

     <%= f.label :email %> 
     <%= f.email_field :email %> 

     <%= f.label :password %> 
     <%= f.password_field :password %> 

     <%= f.label :password_confirmation %> 
     <%= f.password_field :password_confirmation %> 

     <div class="form-actions"> 
     <%= f.submit "Sign up", class: "btn btn-large btn-primary" %> 
     </div> 
    <% end %> 

    <%= render "devise/shared/links" %> 

    </div> 
</div> 

我不能看到什么错,为什么名字文本字段将不会出现......

谢谢您的帮助

回答

5

想通了,它是如此简单,我心疼......所以在这里,它是:

我忘了,因此最终看起来有点像这样生成的表格上一个新的配置文件:

. 
. 
. 

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> 
     <%= render 'shared/error_messages', object: resource %> 

     <div class="invisible"> 
     <%= resource.build_profile %> 
     </div> 

     <%= f.fields_for :profile do |profile_form| %> 
     <%= profile_form.label :full_name %> 
     <%= profile_form.text_field :name %> 

     <% end %> 

     <%= f.label :email %> 
     <%= f.email_field :email %> 

. 
. 
. 

希望这能解决别人的问题!

谢谢!

+1

我只添加对后人说resource.build_profile可能会更好地放置在控制器中。 –