Reputation:
I have learnt that concern folder is basically thin our controllers and models. On the other hand, ApplicationController
has also the same purpose.
We put the common code between model/controller in concern folder and application controller file also does same.
Then what is the difference between them?
Upvotes: 0
Views: 759
Reputation: 34338
Concerns
are basically modules that get mixed into controller or model classes for instance. A Concern
extends ActiveSupport::Concern module. It helps slim down the model/controller classes, and makes it easier to reuse common code across multiple model/controller classes. Concerns are like helper modules. We can define helper methods in concerns and can include or extend those concerns from various controller/model classes to sharing and DRY up the model/controller code.
On the other hand, ApplicationController
is a controller class
(NOT a module
) and in your Rails application, it is the base class
from which all of your other controllers classes are derived from. e.g.
class FooController < ApplicationController
. . .
end
class BarController < ApplicationController
. . .
end
In, Model–View–Controller (MVC) software architectural pattern, a controller implements business logics, can send commands to the model to update the model's state. It can also send commands to its associated view to change the view's presentation of the model. ApplicationController
is that kind where as the Concerns
are like helpers to get their job done.
Upvotes: 0
Reputation: 36860
A concern for controllers can be applied to, perhaps, two or three controllers but you may not want it for all controllers. This is the nice feature of conerns... you can DRY up the code for the controllers that need it, without adding the code to controllers that don't need it.
Code in the application controller is accessible in ALL controllers.
Upvotes: 2