简体   繁体   中英

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.

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.

Concerns are modules that get mixed into controller or model classes for instance. DHH's post focused on models, but the same applies to controllers. It helps slim down the model or controller classes, and makes it easier to reuse common code across multiple classes.

Helpers are for short presentation-oriented methods that you want to call from your views, or very occasionally from your controllers, maybe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM