Я пытался зарегистрировать пользователя и сохранить данные его профиля в другой таблице, потому что у меня их разные типы. Когда они регистрируются, их просят разные поля в зависимости от того, кто они (клиент, бизнес и т. Д.), Я не могу заставить это работать.
Вот мои модели
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :cliente, dependent: :destroy
before_create :build_cliente
accepts_nested_attributes_for :cliente
end
И модель клиента
class Cliente < ApplicationRecord
belongs_to :user
end
Контроллер My Application
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_user!
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [address_attributes: [:nombre, :apellidos]])
end
end
Форма:
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</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>
<%= f.fields_for :cliente do |cliente_form| %>
<div class="form-group">
<%= cliente_form.text_field :nombre, class: "form-control", placeholder: "nombre" %>
<%= cliente_form.text_field :apellidos, class: "form-control", placeholder: "apellidos" %>
</div>
<% end %>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "users/shared/links" %>
Я не знаю, почему, но он делает регистр в таблице клиентов, но у него есть только идентификатор по умолчанию и идентификатор_пользователя, на который правильно ссылаются. Я новичок в рельсах и в кодировании в целом. Я немного растерялся. Любая помощь будет оценена. Кроме того, я знаю, что мой английский немного ржавый, надеюсь, вы понимаете этот пост.