In our rails 3.2.8 app, there is an mountable engine authentify which handles the authentication of users and generates sessions variable for whole app. When we are integrating authentify with another rails engine rfqx, engine rfqx needs to access the session variable generated by engine authentify. The problem is that session varailable is not available in engine rfqx and is nil.
In rfqx's routes.rb, authentify is mounted as follows:
mount Authentify::Engine => "/authentify"
How can we make the session variable available in engine rfqx?
From the Rails Edge guide:
4.3.2 Using a controller provided by the application
Because Rails controllers generally share code for things like authentication and accessing session variables, by default they inherit from ApplicationController. Rails engines, however are scoped to run independently from the main application, so each engine gets a scoped ApplicationController. This namespace prevents code collisions, but often engine controllers should access methods in the main application's ApplicationController. An easy way to provide this access is to change the engine's scoped ApplicationController to inherit from the main application's ApplicationController. For our Blorgh engine this would be done by changing app/controllers/blorgh/application_controller.rb to look like:
class Blorgh::ApplicationController < ApplicationController
end
By default, the engine's controllers inherit from Blorgh::ApplicationController. So, after making this change they will have access to the main applications ApplicationController as though they were part of the main application.
This change does require that the engine is run from a Rails application that has an ApplicationController.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With