The best way to break up a large Rails controller

Currently, I already have a large controller, which is getting bigger. I was wondering what would be the best way to reduce my controllers. I'm not necessarily looking for the easiest way, but a safe and effective way. I have been developing with Rails for a while, but Im still not familiar with how the "subclassification" works, and I'm not even sure that it is supposed to be used that way. I was thinking maybe something like this?

class SomeController < ApplicationController end class MoreFunctionsController < SomeController end 

Currently unverified - I'm still working on it right now, but I hope this can give you an idea of ​​which direction I'm trying to go. I also don't know what the routing will look like for this. What would be the best way to β€œsmash” a large controller?

+7
ruby-on-rails
source share
1 answer

ActiveSupport::Concern (documentation) is what you are looking for.

Refresh

Something like that:

 # config/application.rb config.autoload_paths += %W(#{Rails.root}/app/controllers/concerns) # in Rails4 this is automatic # app/controllers/my_controller.rb class MyController < ApplicationController include GeneralStuffConcern def index render text: foo end end # app/controllers/concerns/general_stuff_concern.rb module GeneralStuffConcern extend ActiveSupport::Concern def show redirect_to root_path end protected def foo 'fooo' end end 

update 2

I really recommend this more http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/

+9
source share

All Articles