I m个新的铁路。 Im利用服务层保持我的控制器 thin。 我的所有服务层档案都位于<条码>、应用程序/服务/单>、<条码>申请/服务/应用程序代码>、<条码>申请/服务/基础设施代码>。 例如,这里是我的公司服务:
class CompanyService
def self.create(params)
company = Company.new(params)
rst = true
ActiveRecord::Base.transaction do
begin
company.save!
rescue ActiveRecord::RecordInvalid
rst = false
rescue ActiveRecord::StatementInvalid
rst = nil
end
end
return company, rst
end
def self.update(params)
company = get_company(params[:id])
rst = true
ActiveRecord::Base.transaction do
begin
company.old_category_ids = company.category_ids
company.assign_attributes(params[:company])
decrease_category_ids = company.old_category_ids-company.category_ids
decrease_counters(decrease_category_ids)
increase_category_ids = company.category_ids-company.old_category_ids
increase_counters(increase_category_ids)
company.save!
rescue ActiveRecord::RecordInvalid
rst = false
rescue ActiveRecord::StatementInvalid
rst = nil
end
end
return company, rst
end # end update
这里是公司控制员:
def create
@company, rst = CompanyService.create(params[:company])
if rst == true
redirect_to(admin_companies_url, notice: "Company was successfully created.")
elsif rst == false
render active_admin_template( new.html.erb )
else
redirect_to admin_companies_url, notice: "Something went wrong. Please try again."
end
end
def update
@company, rst = CompanyService.update(params)
if rst
redirect_to admin_company_url(company), notice: "Company was successfully updated."
elsif rst == false
render active_admin_template( edit.html.erb )
elsif rst == nil
redirect_to admin_companies_url, notice: "Something went wrong. Please try again."
end
end
def destroy
CompanyService.destroy(params[:id])
redirect_to admin_companies_url
end
因此,我有两个问题:
- I know my controller code is not good. How to improve it?
- My services are not automatically loaded in production and development environment. Why?
英语穷人职业。 感谢你提供一切咨询和帮助。