Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

crespire
crespire

Posted on

     

Hooking in to Devise controller actions

Devise is a fantastic authentication gem in the Rails ecosystem that I'm learning more about every day!

I wanted to share how I was able to solve a problem effectively after reading some Devise source code, as it might come in handy for others.

The problem

I was working on a project using the Devise additioninvitable and I needed a way to update a user's profile after they accepted an invite so I could set the slug based on their name. When the user invite is created, we use a generic value as a filler for the name.

I had originally used an ActiveRecord callback, but this felt clunky. Not only was it difficult to understand what it was doing, the side effect of the callback happened on theProfile model! That would be hard to debug if it was your first time working on the app and something was going wrong with the Profile's slug generation.

I began searching for ways to do what I wanted without callbacks and I decided to take a look at the Devise controllers source code.

The solve

It turns out, Devise anticipates that you might want to access a resource while it's doing its thing, and so almost every Devise controller action has a line like this:

yieldresourceifblock_given?
Enter fullscreen modeExit fullscreen mode

This is fantastic! Because we often inherit from the base Devise controllers, what this line does is let you access the resource under operation if you pass in a block:

# sample controllerclassUsers::RegistrationsController<Devise::RegistrationsControllerdefupdatesuper{|resource|...}endend
Enter fullscreen modeExit fullscreen mode

This was a game changer! I now had a great way to update the profile's slug with the user's provided name:

# app/controllers/users/registrations_controller.rbdefupdatesuper{|user|user.profile.update(slug:user.full_name.parameterize)}end
Enter fullscreen modeExit fullscreen mode

Just like that, I was able to get rid of an ActiveRecord callback! So, if you ever need to access the resource to do something inside a Devise controller, this is your ticket.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
joshdevhub profile image
Josh Smith
Ruby/Rails Engineer. Dog Lover. Coffee Drinker. Unoriginal Bio Creator.I enjoy building things and contributing to The Odin Project and The Agency of Learning :>
  • Location
    Portland, OR
  • Joined

Awesome post 💪

Learning how to read library source code can be such an underrated skill

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Full Stack Rails Developer | Rubyist | Open Source Contributor | Dog Dad
  • Location
    Toronto, Canada
  • Education
    University of Toronto
  • Pronouns
    he/him/his
  • Work
    Software Developer at Switch Growth
  • Joined

More fromcrespire

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp