Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put reusable methods for access by controllers in rails

I have several methods I call from my controllers that feel like they should be pulled out and put into a reusable class, outside of the controller. Where do people usually put this stuff? I know I can put them into my ApplicationController, but that doesn't seem to be a great solution if I think I can use these methods later in other applications.

Also, I have a bunch of utility methods in my controllers that likely won't be used in other controllers, or in the future at all, but I feel like they just bloat my controller a bit. Do people usually move these out someplace for cleanliness or just end up with a huge controller?

I'm coming from Java and Actionscript where I'd just create new util classes for this stuff.

like image 723
99miles Avatar asked Apr 28 '10 05:04

99miles


People also ask

What decides which controller receives which requests in Rails?

I fetched this definition from the official rails guide: A controller's purpose is to receive specific requests for the application. Routing decides which controller receives which requests. Often, there is more than one route to each controller, and different routes can be served by different actions.

What are controller callbacks in rails?

Rails provides before and after actions in controllers as an easy way to call methods before or after executing controller actions as response to route requests.

What does before action do in Rails?

The only option of before_action defines one action OR a list of actions when the method/block will be executed first. The set_newsletter_email method will be called just before the show and edit actions. The opposite option except define when NOT to execute the method/block.

What is application controller in Rails?

The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model. The controller is also a home to a number of important ancillary services. It is responsible for routing external requests to internal actions.


2 Answers

The lib directory is a place you can put modules/classes which can be mixed in or used by controllers (and anything else, really). This can be a place to put code that doesn't fall into other areas (but be careful about making sure lib doesn't turn into a mess itself). Side comments just to keep in mind:

  • If you know you have a large amount of related functionality that could, or will, be used in other applications, that might be a plugin.

  • Its also worth keeping in mind that there's nothing wrong with creating a model that is not an Active Record object. So again, depending on what you have, this might make sense as well.

like image 92
Pete Avatar answered Sep 22 '22 21:09

Pete


Create a module file in lib directory:

module ControllerUtil   def foo   end    def bar   end end 

Include the module in the controller:

class UsersController < ApplicationController   include ControllerUtil end 
like image 34
Harish Shetty Avatar answered Sep 25 '22 21:09

Harish Shetty