In my rails model, i define a class and a method to return me an array of Project names. Now my question is, should I, is it correct to use the Project.names array in my view, or should I generate an array in the controller, and pass with an instance variable.
class Project < ActiveRecord::Base
...snip...
def self.names
Project.select(:name).map {|x| x.name }
end
end
From practical perspective, it is always a hard decision where to draw the line between view layer and controller logic. It could be questionable to fill your controller with the likes of:
Only to use the
@procject_namesin the view.But keeping that code out from views would give you later the opportunity to do this without changing the view:
Also, take look at the Draper gem which builds upon the
Decorator pattern and ViewModel of the MVVM pattern.
With Draper, you can keep even your controllers cleaner and have multiple decorators for same object depending on the need (e.g one for web, other for mailer) while still using same view code to render output.
Common thing to place in the Decorator is localized dates which depend on logged in user, therefore don’t fit into view, but would clutter the controller with view-layer logic.