Ruby on rails 如何清理代码并消除NoMethodError异常
当用户创建帐户时,代码应允许该用户通过Desive注册用户帐户,然后再创建一个帐户和一个公司 代码应该重定向到使用wicked gem的向导设置。 用户具有角色(rolify)并通过Cancancan获得授权 这些表是使用has\u many:通过关联设置的。这似乎奏效了 模型如下:Ruby on rails 如何清理代码并消除NoMethodError异常,ruby-on-rails,wicked-gem,Ruby On Rails,Wicked Gem,当用户创建帐户时,代码应允许该用户通过Desive注册用户帐户,然后再创建一个帐户和一个公司 代码应该重定向到使用wicked gem的向导设置。 用户具有角色(rolify)并通过Cancancan获得授权 这些表是使用has\u many:通过关联设置的。这似乎奏效了 模型如下: user has_many :accounts_users has_many :accounts, through: :accounts_users, dependent: :destroy ha
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方法错误
索引和控制器的使用方式:
before_action :set_company, only: [:show, :edit, :update, :destroy,
:新]
这似乎奏效了。编辑公司是一项挑战,但应该做到这一点
欢迎提供任何关于使用更好代码的建议您是否考虑过让前端处理尚未设置
#name
的情况
<=% @company&.name %>
或
我认为您正在使这个不必要的问题变得复杂。一个用户可以拥有一个以上的帐户吗?您的AccountsController创建操作有很多if-else语句。在说@account.save
的行之前,您有一个if-else语句,如果if语句为true,则您没有设置@account,因此您甚至无法保存它…好的。为了简单起见,我正在修改它。但是:我需要该帐户有许多用户,但只有一个公司。用户可以有多个帐户。
<=% @company&.name %>
<=% @company.name || "Unknown" %>