简体   繁体   中英

Adding custom callback to user model with devise

I'm trying to add this callback to my User model, which I generated using Devise.

  before_save :check_invite_code

  def check_invite_code
    if self.invite_code == 'first20'
      User.save
    else
      {icon: 'error', message: "Sorry that's not a valid invite code"}
    end
  end

The issue I am having is with passing the returned hash with the else block back to my view. Typically I'd be able to use the icon and message in the flash in my controller. I'm not sure how to do that. I don't have a UsersController because devise takes care of a the routing with the path being devise/controller#action . So do I create a devise directory inside of controllers, then create the corresponding controllers like sessions, etc and override the devise methods? Looking for some guidance from someone with devise experience.

If you want to customize the UserController you can easily do that by

rails generate devise:controllers [scope]

For example

rails generate devise:controllers users

Check out the documentation here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM