Ruby on Rails登录后设计代码 [英] Ruby on Rails Devise code after login
本文介绍了Ruby on Rails登录后设计代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有些Google Googling似乎有一个选择是将Warden回调放在devise.rb代码。我的问题是:
- 这是对的,还是/或者有更好的方法?
- 如果这是正确的方法...
- 如果Warden :: Manager ...方法在Devise.setup的devise.rb或是吗?
- 是否after_authentication我应该使用的回调?我只是检查一下基于用户名的目录是否存在,如果不存在,则创建它。
解决方案
只是子类 Devise的会话控制器,并将您的自定义行为放在那里:
#config / routes .rb
devise_for:users,:controllers => {:sessions => custom_sessions}
然后创建您的控制器:
#app / controllers / custom_sessions_controller.rb
class CustomSessionsController< Devise :: SessionsController
before_filter:before_login,:only => :create
after_filter:after_login,:only => :创建
def before_login
end
def after_login
end
end
I have an RoR app using Devise for logins. There is some code that is executed when a new User record is created, by being put in the user.rb file as an after_create call/macro/whatever. I need to make this code run after each login, instead of running on new user creation.
With some Googling, it seems that one option is to place Warden callbacks in the devise.rb code. My questions are:
- Is this right, and/or is there a better way to do this?
- If this is the right approach ...
- Should the Warden::Manager... method defs go in devise.rb inside of Devise.setup, or after it?
- Is after_authentication the callback I should use? I'm just checking to see if a directory based on the user's name exists, and if not, creating it.
解决方案
Just subclass Devise's sessions controller and put your custom behaviour there:
# config/routes.rb
devise_for :users, :controllers => { :sessions => "custom_sessions" }
And then create your controller like this:
# app/controllers/custom_sessions_controller.rb
class CustomSessionsController < Devise::SessionsController
before_filter :before_login, :only => :create
after_filter :after_login, :only => :create
def before_login
end
def after_login
end
end
这篇关于Ruby on Rails登录后设计代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文