Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass the session variables from one rails 3.2.8 engine to another engine?

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?

like image 440
user938363 Avatar asked Jan 21 '26 12:01

user938363


1 Answers

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.

like image 81
spinriko Avatar answered Jan 25 '26 06:01

spinriko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!