【问题标题】:How to clean code and eliminate the NoMethodError exception如何清理代码并消除 NoMethodError 异常
【发布时间】:2019-10-03 14:40:50
【问题描述】:

当用户创建帐户时,代码应允许该用户通过 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 

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

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

【问题讨论】:

  • 我认为你把这个不必要的复杂化了。一个用户可以拥有超过 1 个帐户吗?您的 AccountsController 创建操作有许多 if else 语句。在说@account.save 的行之前,您有一个 if else 语句,如果 if 语句为真,则您没有设置@account,因此您甚至无法保存它...
  • 好的。为了简单起见,我正在修改它。但是:我需要该帐户有很多用户,但只有一家公司。用户可以拥有多个帐户。

标签: ruby-on-rails wicked-gem


【解决方案1】:

您是否想过让前端处理尚未设置#name 的情况?

<=% @company&.name %>

<=% @company.name || "Unknown" %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多