Ruby on rails 设计:在不同的表中添加用户详细信息

Ruby on rails 设计:在不同的表中添加用户详细信息,ruby-on-rails,devise,Ruby On Rails,Devise,我有两个模型用户和个人 我想坚持使用Desive为用户提供的默认表,只包含电子邮件和密码,并亲自添加用户详细信息 这是我的模型 class Person < ApplicationRecord belongs_to :user end class-Person

我有两个模型用户和个人

我想坚持使用Desive为用户提供的默认表,只包含电子邮件和密码,并亲自添加用户详细信息

这是我的模型

class Person < ApplicationRecord   
belongs_to :user 
end
class-Person
用户模型

 class User < ApplicationRecord
    ...
    has_one :person
    ...
 end
class用户
我还覆盖RegistrationController.rb,使其看起来像这样

class RegistrationsController < Devise::RegistrationsController

  def sign_up_params
  params.require(:user).permit(:first_name, :email, :password, :password_confirmation)
end

end
类注册控制器
这里是风景

<h2>Sign up</h2>

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= f.error_notification %>
  <%= f.fields_for :person do |p| %>
  <%= p.text_field :first_name %>
  <%= p.text_field :last_name %>
  <% end %>

  <div class="form-inputs">
    <%= f.input :email, required: true, autofocus: true %>
    <%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length) %>
    <%= f.input :password_confirmation, required: true %>
  </div>

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

<%= render "devise/shared/links" %>
注册
使用该代码时,它不起作用,并且在注册时也不会更新“人员”列

如何让Deave使用Deave提供的唯一表单在两个模型中添加细节? 在用户表中添加电子邮件和密码的步骤 以及其他详细信息,例如人员表中的名字

简单表单的嵌套模型设置 将
接受
的嵌套属性添加到
用户
模型

class User < ActiveRecord::Base
  has_one :person
  accepts_nested_attributes_for :person
end
class用户
在控制器中更新许可参数:

class RegistrationsController < Devise::RegistrationsController
  def sign_up_params
    params.require(:user).permit(:email, :password, :password_confirmation,
      person_attributes: [:first_name, :last_name])
  end
end
类注册控制器
将视图中的:个人的
f.fields\u更改为:个人的
f.simple\u fields\u


选中

您需要在控制器私有方法中设置强参数,如下所示:

def sign_up_params
  params.require(:user).permit(:first_name, :email, :password, :password_confirmation, person_attributes: [:person_model_attributes])
end
在您的用户模型中添加以下行:

accepts_nested_attributes_for :person

你在问什么?这不是一个真正的问题。如果它不起作用,那么当您尝试此代码时,现在会发生什么?另外,
Person
不应包含
belowns\u to:Person
确定我正在编辑问题以使其更清晰我应用了更改,但视图中的文本字段已消失,我试图编辑UsersController以在新操作中添加@user.people.build,但没有任何结果Maybe
@user.build\u person,除非@user.person
将work@user.build_person解决了问题,现在开始工作了。谢谢,伙计。