简体   繁体   English

设计经过验证的路线

[英]Devise authenticated route

I'm using devise for auth and want to have the authenticated user route to locations#show. 我正在使用devise进行身份验证,并希望将经过身份验证的用户路由到位置#show。 A user belongs to a location. 用户属于某个位置。 I've tried a couple things including this in routes 我在路线上试过了几件事

authenticated :user do
    match 'locations/:id' => 'locations#show', :as => :root
  end

Unfortunately that doesn't change a thing and routes to home#index even when logged in. My full routes file 不幸的是,即使登录,也不会改变一个东西和路由到家庭#index。我的完整路由文件

 authenticated :user do
    match 'locations/:id' => 'locations#show', :as => :root
  end
  root :to => "home#index"
  resources :locations
  devise_for :users
  resources :users

Any ideas on how to have the root show the location id when a user is logged in? 有关如何让root用户登录时显示位置ID的任何想法?

您可以通过在终端中运行以下命令来查看与设计相关的所有可用路由。

rake routes

You could use a root with a different name inside the authenticated block like this: 您可以在经过身份验证的块中使用具有不同名称的根,如下所示:

authenticated :user do
    root to: 'locations#show', :as => authenticated_root
end

I ended up first defining the after sign in path in my applications controller 我最终在我的应用程序控制器中首先定义了登录后路径

def after_sign_in_path_for(resource)
    if resource.class == User
      location_path(resource.location) 
    elsif resource.class == Location
      location_path(resource) 
    end
  end

Then I setup my route like this 然后我像这样设置我的路线

 authenticated :user do
    devise_for :users do
      get "/", :to => "devise/sessions#new"
    end
    root :to => 'devise/sessions#new'
  end  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM