如果用户已经登录,如何将用户重定向到他们的主页?

时间:2016-12-09 19:58:40

标签: redirect routes config root ruby-on-rails-5

我正在使用Ruby 2.3和Rails 5.如果有人访问我的应用程序中的登录页面并且他们已经登录,我想将它们重定向到他们的用户主页。但是,按照这个链接 - Redirect user after log in only if it's on root_path,我无法让它发挥作用。我将这些添加到我的config / routes.rb文件

  root :to => "users/", :constraints => {user_signed_in?}
  root :to => redirect('/login') 

我用这个内容创建了文件lib / authenticated_user.rb

class AuthenticatedUser
  def self.matches?(request)
    user_signed_in?
  end
end

然而,当我访问我的根页面时,我收到此错误

/Users/nataliab/Documents/workspace/sims/config/routes.rb:17: syntax error, unexpected '}', expecting =>

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

我还需要做些什么来实现这个目标?

编辑:这是我完整的config / routes.rb文件,在给出答案后编辑

Rails.application.routes.draw do

  get    '/signup',  to: 'users#new'
  get    '/forgot_password',  to: 'users#forgot_password'
  get    '/login',   to: 'sessions#new'
  post   '/login',   to: 'sessions#create'
  delete '/logout',  to: 'sessions#destroy'

  get '/dashboard' => 'users#show', as: :dashboard
  resources :users

  resources :scenarios do
    get :download
    resources :confidential_memos
  end

  resources :scenario_files, :only => %i[ show ]

  root :to => "/dashboard", constraints: lambda { |user| user.user_signed_in? } 
  root :to => redirect('/login')

  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

我在app / helpers / sessions_helper.rb

中定义了这个
def current_user
  @current_user ||= User.find_by(id: session[:user_id])
end

# Returns true if the user is logged in, false otherwise.
def logged_in?
  !current_user.nil?
end

2 个答案:

答案 0 :(得分:0)

如果您使用gem devise。有两种方法可以做到。

解决方案1:配置您的routes.rb

devise_scope :user do
  authenticated :user do
    root "users#profiles", as: :authenticated_root
  end
  unauthenticated do
    root "users#login", as: :unauthenticated_root
  end
end

注意:路径可能不正确。只是想法。

解决方案2:使用设计方法after_sign_in_path_for。在here

上引用它

<强> UPDATE1

您可以尝试在application.rb

上添加新方法吗?
before_action :user_authenticated

def user_authenticated
  if logged_in?
    return redirect_to user_profiles_path
  else
    return redirect_to user_login_path
  end
end

更新2 注意: 如果您将方法user_authenticated添加到application.rb,则所有页面都需要用户登录。如果你不想要那个。只需将其添加到您想要的控制器/操作中。

另一种方式:你可以显示方法检查用户身份验证的代码吗?我想你可以在那里添加一个重定向链接。

答案 1 :(得分:0)

您的基本问题是您在大括号内提供布尔值无效的布尔值。大括号表示散列或块。 “Rails路由指南”第3.10节(可用here)解释了您可能要执行的操作。第3节中还有其他选项可以提供解决方案。

根据指南和您提供的信息,一个解决方案就是

root :to => "users/", constraints: lambda { |request| user_signed_in?(request) }

根据您的安全解决方案,这关注user_signed_in?方法必须可用,并且必须能够使用HTTP请求识别用户。

编辑: 我使用你的路由表,对它进行了更改,直到Rails喜欢它。它不喜欢前导斜杠,它不喜欢有两个根路径,即使有约束。这是一个推荐的配置,但您可能需要对其进行修改以确保它完全符合您的要求:

  get    'signup',  to: 'users#new'
  get    'forgot_password',  to: 'users#forgot_password'
  get    'login',   to: 'sessions#new'
  post   'login',   to: 'sessions#create'
  delete 'logout',  to: 'sessions#destroy'

  get 'dashboard' => 'users#show', as: :dashboard
  resources :users

  resources :scenarios do
    get :download
    resources :confidential_memos
  end

  resources :scenario_files, :only => %i[ show ]

  get '/', to: 'users#show',  constraints: lambda { |request| user_signed_in?(request) }
  root :to => redirect('/login')

这有我上面提到的关注user_signed_in?方法必须可用,并且必须能够使用HTTP请求识别用户。

<强>建议:

root  to: 'sessions#check'

然后:

def check
  if logged_in?
    return redirect_to user_profiles_path
  else
    #  You could execute the new method here, or:
    return redirect_to user_login_path
  end
end

这不完全是RESTful,如果您愿意这样做,它可以集成到#new会话中。

替代,基于Duyet的建议:

root  to: 'sessions#new'

然后,仅在会议中:

before_action :user_authenticated, only: [:new]

def user_authenticated
  if logged_in?
    return redirect_to user_profiles_path
  end
end