仅在登录系统后重定向用户 root_path

我有
root_path

在我的申请中 Rails, 这不是用户保护,即,它是一个具有输入形式的简单主页。

用户输入系统后,我希望他去
dashboard_path

.

我做到了:


def signed_in_root_path/scope_or_resource/
dashboard_path
end


它似乎被使用了 , 当用户进入系统时,我不希望他去
root_path

, 同时,强制用户返回上一页,如果他试图进入禁区,他要么已经过期的等待时间,或者它尚未进入系统。

IE:

restricted_page -> 登录 -> restricted_page_but_logged_in

我不想改变这种行为 , 这就是我不使用的原因
after_sign_in_path

, 但



如果它是开启的,将其重定向
root_path

, 或任何不需要用户身份验证的路由。

我的问题是它不起作用。 登录系统后,我被重定向回来
root_path

, 正如我所相信的那样,这是由于这一事实
after_sign_in_path

以前作品。

有没有办法做到这一点? 谢!

Edit: 它第二次工作,当我进入系统时,即我去
root_path

, 我进入系统,我收到一条消息 flash 我进入了系统,并再次将用户名和密码介绍给表单
root_path

. 我成功重定向到了
dashboard_path

. 然而,这不是我想要的行为。
已邀请:

冰洋

赞同来自:

只是觉得

您可以定义两个根 url 一个登录 url 这将指示监控面板和第二个未包含的用户,其中指示输入页面

确定另一个根 url 基于一些限制
在 routes.rb

root url 对于注册用户


constraints/AuthenticatedUser/ do 
root :to => "dashboard"
end


root url 对于未登录的用户


root :to=>"users/signin"


然后创建一个类 AuthenticatedUser 在 lib/authenticated_user.rb


class AuthenticatedUser
def self.matches?/request/
user_signed_in?
end
end


现在,如果用户已进入系统 root_url 将指定监控面板,否则它将指示输入页面

您还可以创建两个根源 /尚未测试过它/


root :to => "dashboard", :constraints => {user_signed_in?}
root :to => "users/signin"


阅读更多关于限制的信息
http://edgeguides.rubyonrails. ... aints
笔记

优先 url 根据创作的顺序
首次创建 -> 具有最优先级的资源

快网

赞同来自:

看起来你的问题太复杂了。 如果您陷入路由变量的整乎,它将在未来导致头疼。 我建议使用过滤器 before, 要求登录并使用该参数 except 或跳过此过滤器 before 对于您的目标页面,如果使用单独的控制器。 举个例子:


class ApplicationController < ActionController::Base

before_filter :require_login, :except => :root

def root
# Homepage
end

protected

def require_login
redirect_to login_path and return unless logged_in?
end

end


/确保你有 logged_in? 肯定/

如果使用单独的控制器,它将看起来像这样:


class HomepageController < ApplicationController

skip_before_filter :require_login
before_filter :route

protected

def route
redirect_to dashboard_path and return if logged_in?
end

end


在登录系统后正确路由,它会在创建会话时所做的。 尽管如此,这个设置应该捕捉到进入系统的任何人,试图进入主页,并将其发送到您的监控面板,以及任何试图到达内容的人 /除了任何东西 root/, 并将它们发送到 login_path

君笑尘

赞同来自:

您可以覆盖该方法 after_sign_in_path_for 不丢失所需的行为。


def after_sign_in_path_for/resource_or_scope/
stored_location_for/resource_or_scope/ || dashboard_path
end


我也会施加行动条件 root_path, 它是重定向的if. current_user 存在。


RootController.rb

def index
if current_user
redirect_to dashboard_path
else
super #or whatever
end
end


你也可以使用 before_filter, 如果要将重定向添加到许多未保护的操作。

奔跑吧少年

赞同来自:

我用 Omniauth, 这种方法对我来说很好。 如果您使用其他策略,我相信您可以更改它。

他们进入系统后,只需将它们redire
root_path

, 它会带领他们
dashboard_path

或者您在下面的路由文件中安装的任何其他默认值。

在应用程序控制器中配置您的助手和回调方法:


# application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user

private

def current_user
@current_user ||= User.find/session[:user_id]/ if session[:user_id]
end

def authenticate_user!
unless current_user
redirect_to root_url
end
end
end



before_filter

在有限的控制器中捕捉外人:


# dashboard_controller.rb
class DashboardController < ActionController::Base
before_filter :authenticate_user!

def index
end

# rest of controller here

end


将这两点添加到路线上。 如果不符合条件,则第一个将不会执行。 如果是这样,顶部有优先级,以及所有呼叫
root_url

将被发送到监控面板


# routes.rb
YourAppName::Application.routes.draw do
root :to => 'dashboard#index', :conditions => lambda{ |req| !req.session["user_id"].blank? }
root :to => 'static_page#index'

# the rest of your routes

end

八刀丁二

赞同来自:

我不确定你是否使用 after_filter 或者 before_filter 在某处为您的重定向,但您可以使用 skip_filter 在其进入控制器中。 然后将用户重定向放入本控制器内的过滤器。

https://coderoad.ru/2390178/

君笑尘

赞同来自:

你可以控制它 before_filter 在申请控制器上。

风见雨下

赞同来自:

登录表单位于每个页面上。 /top/sidebar/ 或者你有一个输入页吗?

如果每个页面都有,可以使用
request.referrer

, 找出用户来自哪里。

二哥

赞同来自:

我认为你的解决方案比必要的更复杂。 为什么你不仅仅是这样做的事情,就在入学形式的行动中:


def login
// logic to check whether login credentials authorize user to sign in
// say 'user_signed_in?' is boolean to determine whether user has successfully signed in
redirect_to/user_signed_in? ? dashboard_path : root_path/
end

要回复问题请先登录注册