你好,我使用设计来注册用户,我想在每次用户注册时创建一个与用户相关的个人资料,问题是当我尝试在注册 View 上添加个人资料模型中的人的全名时设备的用户注册,它不显示...

这是我的模型:

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

这是修改后的设计 View :
<% 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>

我看不出为什么名称文本字段不会出现的任何错误...

感谢您的帮助

最佳答案

想通了,这太简单了,我感觉很糟糕……所以这里是:

我忘了在表单上生成一个新的配置文件,所以它最终看起来像这样:

.
.
.

<%= 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 %>

.
.
.

希望这也能解决其他人的问题!

谢谢!

关于devise - Rails 3 fields_for has_one 不使用设计呈现表单字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12335865/

10-16 15:58