Question

I've been looking around for best practices on where to put some helper methods that do all this calculation / sorting for me but I haven't found a definitive answer yet and was wondering if someone had some good insight for me.

Basically I have an action method that takes a string user input, finds similar words to that string, does a bunch of string manipulation, and then ordering to return an array.

I don't know whether I should have module in /lib, make a controller helper module, ... Where I'm looking for some feedback!

But essentially I just want to:

  1. POST a word to a controller action method
  2. Call a helper method or execute some logic on the word outside of the controller
  3. Have that helper method or wherever that logic will be, return to me the result
Was it helpful?

Solution

Just put that method in your application helper, wherever it is called from it will return the resulting values after processing your logic, you should not over complicate things so the good old application helper is a good place to put the common method used in views and in your controllers

OTHER TIPS

I wouldn't call a helper method from your controller. Personally I would use either a module/plain old ruby object or a concern to handle calculations. The controller stores the object in an instance variable so it can be used througout your views.

Also worth noting are decorators, you should take a look at draper: https://github.com/drapergem/draper

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top