Mat*_*usz 5 ruby model-view-controller ruby-on-rails
我想在我的网站前面放一些统计信息,例如:
# jobs/index.html.haml
.panel
.panel-heading
%h4 Statistics
.panel-body
.col-md-9.col-xs-9
%h5.pull-left Users
.col-md-3.col-xs-3
%h5.pull-right= @usercount
.col-md-9.col-xs-9
%h5.pull-left Companies
.col-md-3.col-xs-3
%h5.pull-right= @companycount
.col-md-9.col-xs-9
%h5.pull-left Categories
.col-md-3.col-xs-3
%h5.pull-right= @categorycount
.col-md-9.col-xs-9
%h5.pull-left Total offers gathered
.col-md-3.col-xs-3
%h5.pull-right= @jobcount
Run Code Online (Sandbox Code Playgroud)
目前这些变量都在控制器中设置:
# controller: jobs, action: index
@jobcount = Job.count
@usercount = User.count
@categorycount = Category.count
@companycount = Job.distinct.count('company')
Run Code Online (Sandbox Code Playgroud)
但我相信这是在MVC中做到这一点的最糟糕方式.谁能告诉我应该怎么做?
我会选择这种风格:
@statistics = {
job_count: Job.count,
user_count: User.count,
category_count: Category.count,
company_count: Job.distinct.count('company')
}