使用 Devise on Rails 7 的 Form_for 嵌套配置文件不显示字段

问题描述 投票:0回答:1

尝试向设计新表单添加基本的个人资料用户名属性,但用户名字段未在屏幕上呈现。

设备注册/new.html.haml

= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f|
  .form-group
    = f.label :email, "Email"
    = f.email_field :email, autofocus: true, class: "form-control"

  - f.fields_for :profile do |profile_form|
    .form-group
      = profile_form.label :username, "Username"
      = profile_form.text_field :username, class: "form-control"

  .form-group
    = f.label :password, "Password"
    = f.password_field :password, autocomplete: "new-password", class: "form-control"

  .form-group
    = f.label :password_confirmation, "Confirm Password"
    = f.password_field :password_confirmation, autocomplete: "new-password", class: "form-control"


  .actions
    = f.submit "Sign up", class: "btn btn-primary"

用户型号:

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_one :profile
  accepts_nested_attributes_for :profile, allow_destroy: true
end

型材型号:

class Profile < ApplicationRecord
  belongs_to :user
  validates :username, presence: true, uniqueness: { case_sensitive: false }
end

注册管理员:

class Users::RegistrationsController < Devise::RegistrationsController
  private

  def sign_up_params
    params.require(:user).permit(:email, :password, :password_confirmation, profile_attributes: [:username])
  end

  def account_update_params
    params.require(:user).permit(:email, :password, :password_confirmation, :current_password, profile_attributes: [:username])
  end
end

我尝试了很多东西 - 这就是我目前正在研究的代码。

任何建议都会有帮助。

ruby-on-rails devise
1个回答
0
投票
  def new
    build_resource({})
    resource.build_profile # This ensures a Profile object is created for the user
    respond_with resource
  end

我需要建立个人资料

© www.soinside.com 2019 - 2024. All rights reserved.