Splitting controller

I’m trying to split controller into several files. I tried extracting
methods into separate files and then requiring them in main
controller, but this doesn’t seem to work.

controllers/reports/sales_reports.rb:
class ReportController < ApplicationController
def sales
end
end

controllers/reports/purchases_reports.rb:
class ReportController < ApplicationController
def purchases
end
end

controllers/report_controller.rb:
require ‘reports/sales_reports’
require ‘reports/purchases_reports’
class ReportController < ApplicationController
end

This doesn’t seem to work. What I’m doing wrong?

Warning: I’m a TOTAL Ruby/Rails beginner

you would have to extract the shared methods into modules, not classes
(controllers)
and then require AND include them.

  • require loads the files into the class file
  • include mixes the loaded file’s methods into the class
    Put the modules in /lib directory for the example below. you can also
    put them in a subdir of the controller dir and give the path in the
    require call

sales.rb:
module sales
def sales

end

purchases.rb:
module purchases
def purchases

end
end

controllers/report_controller.rb:
require ‘sales’
require ‘reports’
class ReportController < ApplicationController
include sales
include purchases

…class code …
end

The methods “sales” & “purchases” will then be available in the
ReportController class

Check out Modules Doc for a more detailed explanation like avoiding
namespace problems etc.: Programming Ruby: The Pragmatic Programmer's Guide

my code is most probably wrong in some details, as i said above i’m a
total beginner and the whole syntax stuff is still in the
learning-by-doing process

Thank you, as always the solution was to read ‘Programming Ruby’ :slight_smile:
You suggestion worked, the only error was that module names have to
start with capital letter.