0

当用户创建帐户时,代码应允许该用户通过 Devise 注册用户帐户,然后创建帐户和公司。

代码应该使用 wicked gem 重定向到向导设置。用户具有角色 (rolify) 并获得 Cancancan 的授权。

这些表是使用 has_many :through 关联设置的。这似乎有效。

型号如下:

user
   has_many :accounts_users
   has_many :accounts, through: :accounts_users, dependent: :destroy
   has_one :company, through: :accounts, dependent: :destroy


users_account
    belongs_to :account
    belongs_to :user

account
    resourcify
    has_many :accounts_users
    has_many :users, through: :accounts_users, dependent: :destroy
    has_one :company

company
    belongs_to :account

创建帐户控制器如下:

  def new
    @account = Account.new
  end

def create
    if !current_user.present?
      build_resource(sign_in_params)
      account = Account.find_or_create_by(org_name: params[:user]    [:account])
    else
      @account = current_user.accounts.create!(account_params)
    end

# create a default account and company
    @account.save!
    current_user.add_role 'owner'
    current_account = current_user.accounts.first
# create a company associated with the newly created account
    if current_account.companies.empty?
      company = current_account.companies.create(company_name: params[:org_name])
    else
      company = current_account.companies.first
    end
    resource.update(current_company: company.id)
    respond_to do |format|
      if @account.save
 # redirect to the relevant wizard
        format.html { redirect_to after_account_path(:add_account),     notice: 'Account was successfully created.' }
      else
        format.html { render action: 'new' }
        format.json { render json: @account.errors, status: :unprocessable_entity }
      end
    end
  end

编辑版

  def create
    @account = Account.create(account_params.except(:company))
    @account.save!
    respond_to do |format|
      if @account.save
        create_company
        format.html { redirect_to @account, notice: 'Account was successfully created.' }
        format.json { render :show, status: :created, location: @account }
      else
        format.html { render :new }
        format.json { render json: @account.errors, status: :unprocessable_entity }
      end
    end
  end

  def create_company
    current_user.add_role 'owner'
    current_account = current_user.accounts.first
    if current_account.company.nil?
      current_account.build_company(company_name: params[:org_name])
      @company.save!
    else
      company = current_account.company
    end
    resource.update(current_company: company.id)
  end

包含在应用程序助手中:

  def current_account
    current_user.accounts.first
  end

  def current_company
    current_user.accounts.first.companies.first
  end

它在创建后立即重定向(意外)以显示(显示)公司数据,我收到 nil / no method 错误。

索引和控制器如何:

before_action :set_company, only: [:show, :edit, :update, :destroy,

:新的]

  def index; end

  def show
    @company = Company.find_by(id: params[:id])
  end


  def set_company
    @company = current_account.company
  end 

这似乎有效。编辑公司是一项挑战,但应该做到这一点。

欢迎任何获得更好代码的建议

4

1 回答 1

0

你有没有想过让前端处理#name尚未设置的情况?

<=% @company&.name %>

或者

<=% @company.name || "Unknown" %>
于 2019-10-04T15:07:00.333 回答