【问题标题】:Devise nested form not creating profile设计嵌套表单不创建配置文件
【发布时间】:2017-01-13 03:47:29
【问题描述】:

Rails 5.0.1
设计 4.2.0

您好,我正在构建一个应用程序,用户必须填写表格才能注册,其中包括设计的注册信息,以及他们个人资料中的一些个人信息。我已经为用户和个人资料制作了模型,建立了它们的一对一关系,并在用户内部添加了accept_nested_attributes_for :profile。我还修改了注册视图,将f.fields_for 包含在个人资料中,直到那时一切似乎都正常。

但是,当我尝试创建新用户并填写所需信息时,我在视图中收到错误消息(我猜来自设计):

1 个错误禁止保存此用户

  • 个人资料用户必须存在

我已经遵循了许多关于如何使用设计创建嵌套表单的指南,但似乎都没有这个问题,我也搜索了很多没有答案。以下是我的注册控制器、用户和配置文件模型以及注册/新视图中的一些 sn-ps:

registrations_controller

class Users::RegistrationsController < Devise::RegistrationsController
  before_action :configure_sign_up_params, only: [:create]
  before_action :configure_account_update_params, only: [:update]

  def new
    build_resources({})
    resource.build_profile
    respond_with self.resource
  end

  protected

  def configure_sign_up_params
    devise_parameter_sanitizer.permit(:sign_up, 
                      keys: [:email, :password,
                      :password_confirmation,
                      profile_attributes: [:name, :art_name,
                      :birth_date, :location]])
  end

用户模型

class User < ApplicationRecord
  has_one :profile, dependent: :destroy
  before_create :build_profile
  accepts_nested_attributes_for :profile

  # Devise configuration.....
end

轮廓模型

class Profile < ApplicationRecord
  belongs_to :user
end

注册/新视图

  <h2>Nueva Cuenta</h2>
  <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
    <%= devise_error_messages! %>

    <%= f.fields_for :profile do |b| %>

      <div class="field">
        <%= b.label :name, "Nombre" %><br />
        <%= b.text_field :name %>
      </div>

      <div class="field">
        <%= b.label :art_name, "Nombre artistico" %><br />
        <%= b.text_field :art_name %>
      </div>

      <div class="field">
        <%= b.label :birth_date, "Fecha de nacimiento" %><br />
        <%= b.date_field :birth_date %>
      </div>

      <div class="field">
        <%= b.label :location, "Ubicacion" %><br />
        <%= b.text_field :location %>
      </div>

    <% end %>

    <div class="field">
      <%= f.label :email %><br />
      <%= f.email_field :email, autofocus: true %>
    </div>

    <div class="field">
      <%= f.label :password %>
      <% if @minimum_password_length %>
        <em>(<%= @minimum_password_length %> characters minimum)</em>
      <% end %><br />
      <%= f.password_field :password, autocomplete: "off" %>
    </div>

    <div class="field">
      <%= f.label :password_confirmation %><br />
      <%= f.password_field :password_confirmation, autocomplete: "off" %>
    </div>

    <div class="actions">
      <%= f.submit "Sign up" %>
    </div>
  <% end %>

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

我的猜测是它与消毒剂有关,我不完全确定如何传递每个属性,因为大多数解释这一点的指南都使用旧版本的设计。

感谢您的关注。

【问题讨论】:

    标签: devise ruby-on-rails-5 nested-forms user-profile


    【解决方案1】:

    在您的个人资料模型中,试试这个:

    belongs_to :user, optional: true

    对我有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      相关资源
      最近更新 更多