Kingsman: Configuring Controllers
Important: These docs are for the outdated Jets 5 versions and below. For the latest Jets docs: docs.rubyonjets.com
If the customization at the views level is not enough, you can customize each controller by following these steps:
-
Create your custom controllers using the generator which requires a scope:
jets generate kingsman:controllers [scope]
If you specify
users
as the scope, controllers will be created inapp/controllers/users/
. And the sessions controller will look like this:class Users::SessionsController < Kingsman::SessionsController # GET /resource/sign_in # def new # super # end ... end
Use the
-c
flag to specify one or more controllers, for example:jets generate kingsman:controllers users -c sessions
) -
Tell the router to use this controller:
kingsman_for :users, controllers: { sessions: 'users/sessions' }
-
Recommended but not required: copy (or move) the views from
kingsman/sessions
tousers/sessions
. Jets will continue using the views fromkingsman/sessions
due to inheritance if you skip this step, but having the views matching the controller(s) keeps things consistent. -
Finally, change or extend the desired controller actions.
You can completely override a controller action:
class Users::SessionsController < Kingsman::SessionsController def create # custom sign-in code end end
Or you can simply add new behavior to it:
class Users::SessionsController < Kingsman::SessionsController def create super do |resource| BackgroundWorker.trigger(resource) end end end
This is useful for triggering background jobs or logging events during certain actions.
Remember that Kingsman uses flash messages to let users know if sign in was successful or unsuccessful. Kingsman expects your application to call flash[:notice]
and flash[:alert]
as appropriate. Do not print the entire flash hash, print only specific keys. In some circumstances, Kingsman adds a :timedout
key to the flash hash, which is not meant for display. Remove this key from the hash if you intend to print the entire hash.