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

最初のステップの 1 つは、さまざまなレポートごとにコントローラーにメソッドを追加することです。これらはあなたの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 ガイド: 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 に答える