Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between a controller helper and a concern?

Would concerns help me share logic for partials? I see Rails 4 has the "concerns" folder for controllers, but it's empty on app generation.

DHH wrote a blog on concerns (but I think they are for the model): http://37signals.com/svn/posts/3372-put-chubby-models-on-a-diet-with-concerns

I would appreciate a concise example.

like image 437
chrisp Avatar asked Sep 11 '13 20:09

chrisp


People also ask

What are rails concerns?

A Rails Concern is a module that extends the ActiveSupport::Concern module. Concerns allow us to include modules with methods (both instance and class) and constants into a class so that the including class can use them.

What are helpers in Rails?

A helper is a method that is (mostly) used in your Rails views to share reusable code. Rails comes with a set of built-in helper methods. One of these built-in helpers is time_ago_in_words . This method is helpful whenever you want to display time in this specific format.


1 Answers

Simply put:

Helpers contain methods to be used in views.

Concerns are modules to be included in controllers/models(depending which concern is it) etc.

It's just a folder with code that you could otherwise put in another place if you wanted. We use fancy terms like concerns to get paid more.

like image 80
Helsing Avatar answered Sep 19 '22 21:09

Helsing