将其他页面添加到Rails控制器?

时间:2012-05-01 19:59:46

标签: ruby-on-rails-3 routes

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

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中的报告拆分到他们自己的页面中。

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

任何指针都将不胜感激!

3 个答案:

答案 0 :(得分:2)

您的第一步是在控制器中为每个不同的报告添加方法。这些内容放在config/routes.rb文件中:

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

这样做的结果是为您提供报告的路径:

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

然后,在您的erb中使用_path变量来引用报告的最佳方式如下:

<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>

这个引物名为"Ruby on Rails Guides: Rails Routing from the Outside In"

,是您开始的好地方

答案 1 :(得分: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

答案 2 :(得分:0)

另一种方法可能是

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

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