Devise Apartment создать арендатора на или после разработки пользователя создать - PullRequest
2 голосов
/ 13 мая 2019

У меня есть небольшое приложение, в котором используются устройства и апартаменты.

У меня есть Пользователь (разработчик), у которого есть одна организация (имя_пользователя).

В организации есть один: владелец, имя_класса 'пользователь'

Я хочу использовать регистрационную форму Devise, чтобы также создать арендатора и назначить его администратору. Я думаю, что прочитал много уроков по разработке квартиры / разработке пользовательских контроллеров / разработке вложенных атрибутов, которые я запутал сам.

Регистрационная форма приложение / просмотров / изобрести / регистрация / new.html.erb

<div class="row">
  <div class="col-lg-4 col-md-6 ml-auto mr-auto">
    <h1 class="text-center">Sign Up</h1>

    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
      <%= render partial: 'devise/shared/error_messages', resource: resource %>

      <div class="form-group">
        <%= f.email_field :email, autofocus: false, class: 'form-control', placeholder: "Email Address" %>
      </div>
      <div class="form-group">
        <%= f.simple_fields_for :organizations do |o| %>
          <%= o.input :name, placeholder: "Organization Name", warning: "Cant Be Changed", label: false  %>
        <% end %>
      </div>

      <div class="form-group">
        <%= f.password_field :password, autocomplete: "off", class: 'form-control', placeholder: 'Password' %>
      </div>

      <div class="form-group">
        <%= f.password_field :password_confirmation, autocomplete: "off", class: 'form-control', placeholder: 'Confirm Password' %>
      </div>

      <div class="form-group">
        <%= f.submit "Sign up", class: "btn btn-primary btn-block btn-lg" %>
      </div>
    <% end %>

    <div class="text-center">
      <%= render "devise/shared/links" %>
    </div>
  </div>
</div>

Модель пользователя

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_one :organization, dependent: :destroy
  after_create :init_organization
  accepts_nested_attributes_for :organization

  private

  def init_organization
    self.create_organization!
  end



end

Модель организации

class Organization < ApplicationRecord
    has_one :owner, class_name: 'User'
    has_many :organizations_users
    has_many :users, through: :organizations_users
    has_many :clients

    after_create :create_tenant

    def tenant_name
      "#{self.id}"
    end

    private

    def create_tenant
      Apartment::Tenant.create(self.tenant_name)
    end
end

Я знаю, что мне нужно изменить свой метод создания на моем контроллере devise, поэтому я сгенерировал собственные контроллеры devise с помощью здесь

rails generate devise:controllers users

и добавили пользовательские дезинфицирующие средства

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

  # GET /resource/sign_up
  # def new
  #   super
  # end

  # POST /resource
  def create
    super
  end

  # GET /resource/edit
  # def edit
  #   super
  # end

  # PUT /resource
  # def update
  #   super
  # end

  # DELETE /resource
  # def destroy
  #   super
  # end

  # GET /resource/cancel
  # Forces the session data which is usually expired after sign
  # in to be expired now. This is useful if the user wants to
  # cancel oauth signing in/up in the middle of the process,
  # removing all OAuth session data.
  # def cancel
  #   super
  # end

  # protected

  # If you have extra params to permit, append them to the sanitizer.
  def configure_sign_up_params
   devise_parameter_sanitizer.permit(:sign_up, keys: [:email, organizations: [:name]])
  end

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_account_update_params
  #   devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
  # end

  # The path used after sign up.
  # def after_sign_up_path_for(resource)
  #   super(resource)
  # end

  # The path used after sign up for inactive accounts.
  # def after_inactive_sign_up_path_for(resource)
  #   super(resource)
  # end
end

схема

ActiveRecord::Schema.define(version: 2019_05_12_083957) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "pgcrypto"
  enable_extension "plpgsql"
  enable_extension "uuid-ossp"

  create_table "Organizations_Users", id: false, force: :cascade do |t|
    t.uuid "Organization_id", null: false
    t.uuid "User_id", null: false
  end

  create_table "clients", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name"
    t.uuid "organization_id", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["organization_id"], name: "index_clients_on_organization_id"
  end

  create_table "equipment", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name"
    t.uuid "site_id", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["site_id"], name: "index_equipment_on_site_id"
  end

  create_table "organizations", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.uuid "user", null: false
  end

  create_table "sites", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name"
    t.uuid "client_id", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["client_id"], name: "index_sites_on_client_id"
  end

  create_table "users", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

  add_foreign_key "clients", "organizations"
  add_foreign_key "equipment", "sites"
  add_foreign_key "sites", "clients"
end

когда я прохожу процесс регистрации, я получаю неизвестный атрибут «организаций» для пользователя.

Обновление:

Я добавил две новые миграции, чтобы добавить ссылку на пользователя в организации и организацию для пользователя.

Я сделал это, чтобы создать связь для владельца, если бы я позвонил референтному владельцу

class AddUserToOrganization < ActiveRecord::Migration[5.2]
  def change
    add_reference :organizations, :user, type: :uuid, null: false, index: true, foreign_key: true
  end
end

class AddOrganizationToUser < ActiveRecord::Migration[5.2]
  def change
    add_reference :users, :organization, type: :uuid, null: false, index: true, foreign_key: true
  end
end

Моя схема теперь выглядит так:

ActiveRecord::Schema.define(version: 2019_05_13_223120) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "pgcrypto"
  enable_extension "plpgsql"
  enable_extension "uuid-ossp"

  create_table "Organizations_Users", id: false, force: :cascade do |t|
    t.uuid "Organization_id", null: false
    t.uuid "User_id", null: false
  end

  create_table "clients", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name"
    t.uuid "organization_id", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["organization_id"], name: "index_clients_on_organization_id"
  end

  create_table "equipment", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name"
    t.uuid "site_id", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["site_id"], name: "index_equipment_on_site_id"
  end

  create_table "organizations", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.uuid "user_id", null: false
    t.index ["user_id"], name: "index_organizations_on_user_id"
  end

  create_table "sites", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name"
    t.uuid "client_id", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["client_id"], name: "index_sites_on_client_id"
  end

  create_table "users", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.uuid "organization_id", null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["organization_id"], name: "index_users_on_organization_id"
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

  add_foreign_key "clients", "organizations"
  add_foreign_key "equipment", "sites"
  add_foreign_key "organizations", "users"
  add_foreign_key "sites", "clients"
  add_foreign_key "users", "organizations"
end

Я следовал указаниям, чтобы изменить свой контроллер пользователя / регистраций на:

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

  def new
    super
    @organization = Organization.new
  end

  # POST /resource
  def create
    super
  end

  # GET /resource/edit
  # def edit
  #   super
  # end

  # PUT /resource
  # def update
  #   super
  # end

  # DELETE /resource
  # def destroy
  #   super
  # end

  # GET /resource/cancel
  # Forces the session data which is usually expired after sign
  # in to be expired now. This is useful if the user wants to
  # cancel oauth signing in/up in the middle of the process,
  # removing all OAuth session data.
  # def cancel
  #   super
  # end

  # protected

  # If you have extra params to permit, append them to the sanitizer.
  def configure_sign_up_params
   devise_parameter_sanitizer.permit(:sign_up, keys: [:email, organizations: [:name]])
  end

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_account_update_params
  #   devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
  # end

  # The path used after sign up.
  # def after_sign_up_path_for(resource)
  #   super(resource)
  # end

  # The path used after sign up for inactive accounts.
  # def after_inactive_sign_up_path_for(resource)
  #   super(resource)
  # end
end

и моя форма сейчас

<%= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name), :html => {:class => 'form-horizontal' }) do |f| %>
      <%= render partial: 'devise/shared/error_messages', resource: resource %>

        <%= f.input :email, autofocus: false, class: 'form-control', placeholder: "Email Address", label: false  %>
        <%= f.simple_fields_for :organization do |o| %>
          <%= o.input :name, placeholder: "Organization Name", warning: "Cant Be Changed", label: false  %>
        <% end %>
        <%= f.input :password, autocomplete: "off", class: 'form-control', placeholder: 'Password', label: false  %>
        <%= f.input :password_confirmation, autocomplete: "off", class: 'form-control', placeholder: 'Confirm Password', label: false  %>
        <%= f.button :submit, "Sign up", class: "btn btn-primary btn-block btn-lg" %>
    <% end %>

Моя проблема сейчас в том, что я не вижу поля названия организации в представлении формы.

1 Ответ

1 голос
/ 13 мая 2019

В отношении has_one вы должны использовать форму единственного числа:

  <div class="form-group">
    <%= f.simple_fields_for :organization do |o| %>
      <%= o.input :name, placeholder: "Organization Name", warning: "Cant Be Changed", label: false  %>
    <% end %>
  </div>

Я думаю, что остальная часть вашего кода должна работать как есть!

Редактировать: как создать новую организацию

Вы указали, что форма пуста, потому что нет экземпляра org. Сделайте следующее в вашем пользовательском контроллере:

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

  GET /resource/sign_up
  def new
    super
    @user.organization = Organization.new
  end

  # POST /resource
  def create
    super
  end
end
...