如何在 rails 中通过 Clearance 注册时创建帐户并将其分配给用户

How to create an account when and assign it to a User when signing up via Clearance in rails

使用 Clearance 自动创建具有唯一 account_id(不必是 UUID)并在注册时将其分配给新用户的 "Account" 的最佳方法是什么?

我需要延长许可用户注册表单和用户控制器来处理这个问题,但我无法让它正常工作。

这应该是 before_filter 注册还是用户创建操作的一部分?


更新:添加一些代码以供参考...

# app/controllers/users_controller.rb
class UsersController < Clearance::UsersController

  def create
    @user = user_from_params
    @account = account_from_params
    @user.account = @account

    if @user.save
      sign_in @user
      redirect_back_or url_after_create
    else
      render template: "users/new"
    end
  end

end

class AccountController < ApplicationController

  def create
    @account = Account.new(account_params)
  end

  private

  def account_params
    params[:account].permit(:id)
  end

end

<fieldset>
  <%= form.label :email %>
  <%= form.text_field :email, type: 'email' %>
</fieldset>

<fieldset>
  <%= form.label :password %>
  <%= form.password_field :password %>
</fieldset>


# Users & Clearance routes
  resources :passwords, controller: 'clearance/passwords', only: [:create, :new]
  resource :session, controller: 'clearance/sessions', only: [:create]

  resources :users, controller: 'clearance/users', only: [:create] do
    resource :password,
      controller: 'clearance/passwords',
      only: [:create, :edit, :update]
  end
  get '/login' => 'clearance/sessions#new', as: 'sign_in'
  delete '/logout' => 'clearance/sessions#destroy', as: 'sign_out'
  get '/signup' => 'clearance/users#new', as: 'sign_up'
  get '/users' => 'users#index'

我会连接到他们的控制器来保持东西 "in the gem" 可以这么说。 This is how it works。看起来你会通过像这样覆盖它们来连接到控制器:

class UsersController < Clearance::UsersController

  def create
    @user = user_from_params
    @account = account_from_params
    @user.account = @account

    if @user.save
      sign_in @user
      redirect_back_or url_after_create
    else
      render template: "users/new"
    end
  end

end

不要忘记更新路由并将它们指向此控制器操作。现在,您有点想从 Clearance 中解开控制器 create 操作,但您是在 "Clearance way" 中进行操作,这似乎是最合适的。