# Splitting controller

**URL:** <https://www.ruby-forum.com/t/splitting-controller/72669>\
**Category:** Rails\
**Created:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/splitting-controller/72669 "2006-10-18T10:40:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Domas\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc348/32.png) [@Domas\_S](https://www.ruby-forum.com/u/Domas_S)\
**Post date:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/splitting-controller/72669/1 "2006-10-18T10:40:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Domas\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc348/32.png) [@Domas\_S](https://www.ruby-forum.com/u/Domas_S)\
**Post date:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/splitting-controller/72669/2 "2006-10-18T10:40:33Z")

</div>

## 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](http://www.ruby-doc.org/docs/ProgrammingRuby/)

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

---

<div class="post-metadata">

**Author:** ![Domas\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc348/32.png) [@Domas\_S](https://www.ruby-forum.com/u/Domas_S)\
**Post date:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/splitting-controller/72669/3 "2006-10-18T10:40:37Z")

</div>

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