覆盖Devise的注册控制器,以便在成功登录完成后允许重定向 [英] Overriding Devise's registration controller to allow for a redirect after a successful sign_up has been done
问题描述
我知道你是想覆盖注册控制器,像这样:
class Users :: RegistrationsController< Devise :: RegistrationsController
def after_sign_up_path_for(资源)
authors_waiting_path
end
end
然后按照Tony Amoyal展示的示例 http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/ ,我应该更改我的路由来更新新控制器的访问权限:
devise_for:users,:controllers = > {:registrationrations =>users / registrations} do
#get'/ author / sign_up',:to =>'d evise / registrations#new'
#get'/ client / sign_up',:to => 'devise / registrations#new'
get'/ author / sign_up',:to => 'users / registrations#new'
get'/ client / sign_up',:to => '用户/注册#新'
结束
是的,我有一些有点奇怪的,因为我正在抓住一些特定的路径将它们发送到注册页面,这样可以有效地创建2个注册场景。
我评论过我在覆盖注册控制器之前的内容。
即使这一切和我的authors_waiting_path都是一个有效的路径,它只是继续注册后的登录页面:(
这真的很令人沮丧。
Alex
编辑:我还在设计维基上发现了这一点: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-after -registration-(注册)
但我不知道在哪里定义这个创建方法?我应该覆盖会话控制器吗?
编辑2:
我把控制器的虚拟覆盖:
class Pouets :: RegistrationsController< Devise :: RegistrationsController
def after_sign_up_path_for(resource)
authors_waiting_path
end
def new
super
end
def create
putswas here
super
end
def edit
super
end
def update
super
end
def destroy
super
end
def取消
超级
结束
结束
我从来没有在我的日志中的在这里....我真的有这样的感觉,它完全忽略了覆盖...我一定在做错事:(
Ok ...我可以覆盖它,所以你应该是:0
创建文件夹应用程序/控制器/用户
将这些注册用于:registrations_controller.rb:(与会话选项 - 但它会尝试sign_in和更高版本的重定向 - 它可能是不是你的意图行为)。此外,这是来自devise wiki,我不知道它是否可行。
class Users :: RegistrationsController< Devise :: RegistrationsController
def create
session [#{resource_name} _return_to] = complete_path
super
end
end
重新启动应用程序(只是为了确保你不信任任何东西)
所有这一切都必须重写创建如果你想重定向用户...如果你想定义一些更复杂的场景,你应该monkeypatch sign_in_and_redirect
所以你的控制器看起来像
class Users :: RegistrationsController< Devise :: RegistrationsController
#POST / resource / sign_up
def create
build_resource
如果resource.save
set_flash_message:notice,,signed_up
#sign_in_and_redirect(resource_name,resource)\
#这个注释行负责登录和重定向
#change到你想要的东西
else
clean_up_passwords(resource)
render_with_scope:new
end
end
end
第二个选项尝试monkeypatch助手....
模块设计
模块控制器
#这些助手是添加到ApplicationController的方便的方法。
模块帮助者
def sign_in_and_redirect(resource_or_scope,resource = nil,skip = false)
#注册行为
end
end
end
end
I have looked all over the place, and found a lot of info... but nothing works for me and I don't get it :(
I know that you are suppose to override the registration controller, like this:
class Users::RegistrationsController < Devise::RegistrationsController
def after_sign_up_path_for(resource)
authors_waiting_path
end
end
Then following the example showed by Tony Amoyal http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/, I am supposed to change my routes to update the access the new controller:
devise_for :users, :controllers => { :registrations => "users/registrations" } do
#get '/author/sign_up', :to => 'devise/registrations#new'
#get '/client/sign_up', :to => 'devise/registrations#new'
get '/author/sign_up', :to => 'users/registrations#new'
get '/client/sign_up', :to => 'users/registrations#new'
end
Yes, I have something a bit strange here, because I am catching some specific path to send them to the registration page, this allows me to create effectively 2 registration scenario. I commented what I had before I had overridden the registration controller.
Even with all this and my authors_waiting_path being a valid path, it just keeps on going to the sign-in page after registration :(
This is really frustrating.
Alex
edit: I also found this on the devise wiki: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-after-registration-(sign-up)
But I have no idea where to define this create method ? should I override the session controller ???
edit 2:
I put a dummy override of the controller:
class Pouets::RegistrationsController < Devise::RegistrationsController
def after_sign_up_path_for(resource)
authors_waiting_path
end
def new
super
end
def create
puts "was here"
super
end
def edit
super
end
def update
super
end
def destroy
super
end
def cancel
super
end
end
And I never the "was here" in my logs.... I really have the feeling that it's totally ignoring the override... I must be doing something wrong :(
Ok... I am able to override it so you should be either :0
Create folder app/controllers/users
put there registrations_controller.rb with: (option with session - but it will try sign_in and later redirect - it may be not intended behavior for you ). Furthermore this is from devise wiki and I am not sure if it works
class Users::RegistrationsController < Devise::RegistrationsController
def create
session["#{resource_name}_return_to"] = complete_path
super
end
end
restart application (just for ensure you don't trust anything)
All in all you must override Create If you want redirect only Users... if you want define some more complex scenario you should monkeypatch sign_in_and_redirect
so your controller will looks like
class Users::RegistrationsController < Devise::RegistrationsController
# POST /resource/sign_up
def create
build_resource
if resource.save
set_flash_message :notice, :signed_up
#sign_in_and_redirect(resource_name, resource)\
#this commented line is responsible for sign in and redirection
#change to something you want..
else
clean_up_passwords(resource)
render_with_scope :new
end
end
end
second option try to monkeypatch helper ....
module Devise
module Controllers
# Those helpers are convenience methods added to ApplicationController.
module Helpers
def sign_in_and_redirect(resource_or_scope, resource=nil, skip=false)
#intended behaviour for signups
end
end
end
end
这篇关于覆盖Devise的注册控制器,以便在成功登录完成后允许重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!