1

我在我的 Rails 3 应用程序中提供了一个名为报告的控制器。控制器的索引部分如下所示:

def index
    #@reports = Report.all

    @clinical_income_by_month = Clinical.income_by_month                      
    @clinical_income_by_employee = Clinical.income_by_employee
    @clinical_income_by_vet = Clinical.income_by_vet                          
    @consult_consult_times = Consult.consult_times
      @clinical_healthplan_high_spenders = Clinical.healthplan_high_spenders

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @reports }
    end
  end

index.html.erb 代码如下所示:

<h6><b>Financial Reports</b></h6>
    <dl class="vertical">
      <dd><a href="/income_by_month">Income by Month</a></dd>
      <dd><a href="/income_by_employee">Income by Employee</a></dd>
      <dd><a href="/income_by_vet">Income by Vet</a></dd>
 </dl>

我已将最初全部在 jQuery 选项卡下的 index.html.erb 中的报告拆分为它们自己的页面。

加载各个报告的链接的最佳方法是什么?目前,如果我访问链接或手动输入http://0.0.0.0:3000/reports/income_by_month.html我会收到未知页面错误。

任何指针将不胜感激!

4

3 回答 3

2

One of your first steps is to add methods in your controller for each of the different reports. These go in your config/routes.rb file:

resource :reports do
  member do
    get 'income_by_month'
    get 'income_by_employee'
    get 'income_by_vet'
  end 
end

The result of this is to give you paths to your reports of:

income_by_month_reports    GET    /reports/income_by_month
income_by_employee_reports GET    /reports/income_by_employee
income_by_vet_reports      GET    /reports/income_by_vet

Then, in your erb the best way to refer to the reports using _path variables like so:

<h6><b>Financial Reports</b></h6>
<dl class="vertical">
  <dd><a href='<%= income_by_month_reports_path %>'>Income by Month</a></dd>
  <dd><a href='<%= income_by_employee_reports_path %>'>Income by Employee</a></dd>
  <dd><a href='<%= income_by_vet_reports_path %>'>Income by Vet</a></dd>
</dl>

A good place for you to start is this primer called "Ruby on Rails Guides: Rails Routing from the Outside In"

于 2012-05-02T02:02:20.723 回答
1

好吧,你可以这样做,通过将一些路由连接到控制器中的新操作:

# in config/routes.rb
map '/income_by_month',    :to => 'reports#income_by_month'
map '/income_by_employee', :to => 'reports#income_by_employee'
map '/income_by_vet',      :to => 'reports#income_by_vet'

# in reports_controller
def income_by_month
  @clinical_income_by_month = Clinical.income_by_month
end
def income_by_employee
  @clinical_income_by_employee = Clinical.income_by_employee
end
def income_by_vet
  @clinical_income_by_vet = Clinical.income_by_vet
end

但在我看来,Rails 的做法更像是这样:

# index.html.erb
<h6><b>Financial Reports</b></h6>
<dl class="vertical">
  <dd><a href="/reports?by=month">Income by Month</a></dd>
  <dd><a href="/reports?by=employee">Income by Employee</a></dd>
  <dd><a href="/reports?by=vet">Income by Vet</a></dd>
</dl>

# in reports_controller
def index
  @clinical_income = case params[:by]
  when 'month'    then Clinical.income_by_month                      
  when 'employee' then Clinical.income_by_employee
  else Clinical.income_by_vet
  end
end
于 2012-05-01T20:22:39.350 回答
0

另一种方法可能是

#config/routes.rb
resources :reports do
  collection do
    get "income_by_month"
    get "income_by_employee"
    get "income_by_vet"
  end
end

并在您的报告控制器中映射这些功能

于 2012-05-01T20:44:52.633 回答