2013-03-28 69 views
2

我安装了设计,它工作正常,但重定向登录。当用户从应用程序的任何页面登录时,Devise会将用户重定向到首页,而不是在同一页面上重定向。我试着实现设计的How_to解决方案,但没有任何帮助。登录重定向到首页,而不是停留在同一页

我application_controller写道:

def after_sign_in_path_for(resource) 
current_user_path 
end 

但它给错误:

undefined local variable or method `current_user_path' for #<Devise::SessionsController:0xaeacc34> 

当我写:

def after_sign_in_path_for(resource) 
current_user_path 
end 

它提供:

undefined method `user_url' 

这个问题的解决方案是什么?任何身体都可以帮助吗?

回答

1

你需要重定向到其他scenerio(错误或成功)的地方?通常在用户登录成功后使用after_sign_in_path_for。 SO:

如果您尚未覆盖设计默认功能,请使用以下代码将控件导航至特定的自定义页面。 current_user也可以在此操作中访问。

def after_sign_in_path_for(resource) 
    scope = Devise::Mapping.find_scope!(resource) 
    scope_path = :"#{scope}_root_path" 
    respond_to?(scope_path, true) ? send(scope_path) : root_url 
end 

更新: 另一个例子如下:

def after_sign_in_path_for(resource_or_scope) 
    current_user.admin? ? dashboard_admin_home_index_path : current_user.sign_in_count >= 1 ? "/home/dashboard" : "/#{current_user.role}/dashboard" 
    end 
+1

为好,它重定向到主页。我需要用户留在他以前的同一页面上。 – user2206724 2013-03-28 08:50:50

1

您可以在范围上面的代码中使用request.referer

def after_sign_in_path_for(resource_or_scope) 
    request.referer 
end 
+0

谢谢!我不知道为什么这没有得到更多upvotes。在提出的所有其他解决方案中,这是唯一为我工作的解决方案 – guero64 2016-11-28 18:24:45