I have a rails engine that expose the following controller:
class ActsAsAssets::AssetsController < ApplicationController
..........
The main application uses devise into the ApplicationController. From the main application I use to extend the Engine Controller normally like this:
class MainApplicationController < ActsAsAssets::AssetsController
.......
What I was aspecting is that MainApplicationController was extending the main application ApplicationController via the engine.
Note that the engine does not have any ApplicationController so I was expecting ActsAsAssets::AssetsController < ApplicationController to actually extend the ApplicationController of the rails app that uses the engine.
Looks like I am wrong.
Any suggestion?
Basically what I want to achieve is that a controller from my main app extends a rails engine controller that extends the main ApplicationController cause does not have one inside the engine.
Hoper is clear.
Change the declaration to look like this:
class ActsAsAssets::AssetsController < ::ApplicationController
That instructs the controller to extend the non-namespaced ApplicationController from the main application.
Related
When upgrading engine from Rails 3.2 to Rails 4.2.0, the following inheritance in application controller causes circular dependency error in rspec:
class ApplicationController < ApplicationController
end
We have config.eager_load = false for config/development.rb.
The error:
activesupport-4.2.0/lib/active_support/dependencies.rb:492:in `load_missing_constant': Circular dependency detected while autoloading con
stant Authentify::ApplicationController (RuntimeError)
Here is the Rails engine document (ch:4.3.2) explaining this type of code practice. As I understand, the purpose of this inheritance is to allow the engine to access methods in Rails app or other engine which the current engine is mounted to. We would like to do the same in Rails 4.2.0 engine. How to fix this problem?
your applicationController is clearly trying to inherit from itself, it should rather look like
class ApplicationController < ActionController::Base
In rails 4 engine, the right format is:
class ApplicationController < ::ApplicationController
end
assume the class is within module MyEngine. Or
class MyEngineName::ApplicationController < ::ApplicationController
end
I am new to Ruby on Rails Development and was trying to understand inheritance in Rails, I understood how do a class inherit from a parent class
For Example MyController < ActionController, in this Action Controller is the parent class. But I dont understand this syntax
ApplicationController < ActionController::Base
Specifically what is the purpose of ::Base
This syntax is used to indicate that Base is a class inside of ActionController namespace.
I have done this command: rails g controller father/child and it has genersted this for me:
class Father::ChildController < ApplicationController
end
But my goal is to have this:
module Father
class ChildController < ApplicationController
end
end
Are these two the same? or I should use generate controller in a different way to achive that module like syntax?
They not the same. The second one is equivalent to the following:
class Father::ChildController < Father::ApplicationController
end
I've decided to break up by Rails application into different modules for clarity, and as a part of this I'd like each module to have its own ApplicationController. So I went through and defined an application controller for each module like so:
module Login
class ApplicationController < ::ApplicationController
...
end
end
Then I'd create a different controller in the login module:
module Login
class HomeController < ApplicationController
end
end
My expectation was that this would first search the Login module for the application controller, but from what I'm seeing it's actually inheriting the global application controller (any declarations I make in Login::ApplicationController aren't picked up). I've searched around for some information on this, but haven't been able to figure out why it is using the ApplicationController in the top level name space and not the one in Login. It works just fine having HomeController inherit from Login::ApplicationController, but I'd like to learn why it doesn't work without the Login:: prefix for the future.
Try
module Login
class HomeController < Login::ApplicationController
end
end
I'm writing a rails application, and in this one controller I need to specify the layout to render due to inheriting from ActionController::Base. In some of my other controllers I just have ActionController and it automatically uses the application layout. Whenever I remove the ::Base, I get the following message when accessing the page superclass must be a Class (Module given).
Why does inheriting from ActionController::Base in this controller matter, but not in the others?
To directly answer your question ActionController is not a controller class, it's a namespacing module that powers the entire controller stack. You would not interact with the ActionController module during typical Rails development. ActionController::Base is actually the class that controllers inherit from. This is why you can't inherit from ActionController.
But I think there are two controllers in play here. ActionController::Base and ApplicationController. I think you might be mistaking ApplicationController for being ActionController without the ::Base.
ActionController::Base is the main controller class where all of your Rails functionality comes from. ApplicationController is a generalized controller that you can add methods to and have all of your other Rails controllers inherit from.
You could do the following to use a different layout in one of your controllers:
class AuthenticationController < ApplicationController
layout 'authentication'
end
You can either use the AuthenticationController directly, or have new controllers inherit from AuthenticationController.
Your controllers should inherit from ApplicationController. This will allow them to automatically render the application layout. ApplicationController extends ActionController::Base.