Reputation: 3814
I gave a controller called reports in my Rails 3 application. The index section of the controller looks like this:
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
The index.html.erb code looks like this:
<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>
I have split the reports which were originally all in index.html.erb under jQuery tabs into their own pages.
What is the best way to make the links to the individual reports load? Currenly if I visit the links or manually enter http://0.0.0.0:3000/reports/income_by_month.html I get an unknown page error.
Any pointers would be appreciated!
Upvotes: 1
Views: 1189
Reputation: 13404
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"
Upvotes: 2
Reputation: 2014
Another approach could be
#config/routes.rb
resources :reports do
collection do
get "income_by_month"
get "income_by_employee"
get "income_by_vet"
end
end
and map these functions in your reports controller
Upvotes: 0
Reputation: 18784
Well, you could do it like this, by wiring up some routes to new actions in your controller:
# 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
But it seems to me, the Rails way to do this would be more like this:
# 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
Upvotes: 1