登录后在站点根目录下显示show page url

时间:2013-11-04 17:14:17

标签: ruby-on-rails devise routes ruby-on-rails-4

目前,当用户登录或注册时,他们会被重定向到/ users / 1,例如,作为他们的展示页面。

我无法确定使用哪些路由将它们重定向到站点根目录(例如,example.com,而不是example.com/users/1)。登录的站点根目录是显示页面,注销的站点根目录将是正常的站点主页。

我正在使用设计,如果重要的话。

目前路线:

  devise_for :users, :path => '', :path_names => { :sign_in => 'login', :sign_out => 'logout',
                                      :password => 'password', :confirmation => 'verification',
                                      :unlock => 'unblock', :registration => 'signup',
                                      :sign_up => 'new' }

  devise_scope :user do
    get   'login',          to: 'devise/sessions#new'
    get   'users/login',    to: 'devise/sessions#new'
    get   'logout',         to: 'devise/sessions#destroy'
    get   'signup',         to: 'devise/registrations#new'
    get   'password',       to: 'devise/passwords#new'
    match 'users/secret',   to: "devise/passwords#create",      via: :post
    match 'sessions/user',  to: 'devise/sessions#create',       via: :post
    match 'users/signup',   to: 'devise/registrations#create',  via: :post
    match 'users/signup',   to: 'devise/registrations#create',  via: :post
  end

  resources :users
  resources :sessions

  root 'site#index'

更新路线:

  devise_for :users, :path => '', :path_names => { :sign_in => 'login', :sign_out => 'logout',
                                      :password => 'password', :confirmation => 'verification',
                                      :unlock => 'unblock', :registration => 'signup',
                                      :sign_up => 'new' }

    get   'login' => 'users/login'

  devise_scope :user do
    get   'login',          to: 'devise/sessions#new'
    get   'users/login',    to: 'devise/sessions#new'
    get   'logout',         to: 'devise/sessions#destroy'
    get   'signup',         to: 'devise/registrations#new'
    get   'password',       to: 'devise/passwords#new'
    match 'users/secret',   to: "devise/passwords#create",      via: :post
    match 'sessions/user',  to: 'devise/sessions#create',       via: :post
    match 'users/signup',   to: 'devise/registrations#create',  via: :post
    match 'users/signup',   to: 'devise/registrations#create',  via: :post
  end

  get '', to: 'users#show', as: 'user'
  get 'edit', to: 'users#edit', as: 'user/edit'

  #resources :users
  resources :sessions

  # Authenticated Users:
  authenticated :user do
    root to: "users#show", as: :authenticated_root
  end

  # Non-Authenticated Users
  root to: 'site#index'

2 个答案:

答案 0 :(得分:2)

routes.rb 中(将目标to:更改为您希望的页面)

YourApp::Application.routes.draw do

  ...

  # Authenticated Users:
  authenticated :user do
    root to: 'user#show', as: :authenticated_root
  end

  # Non-Authenticated Users
  root to: 'site#index'

end

Rails 4修复:https://github.com/plataformatec/devise/issues/2393#issuecomment-17298414

UserController.rb 中:确保在show方法中使用current_user代替params[:id]。这样做是因为通常每当您调用show动作时都会发送id,但在这种情况下,您不会这样做。

class UsersController < ApplicationController
  def show
    if params[:id].present?
      @user = User.find(params[:id])
    else
      @user = current_user
    end
  end
end

答案 1 :(得分:2)

您可以使用authenticatedunauthenticated资源。

像这样:

authenticated :user do
 root to: "users#show", as: :authenticated_root, via: :get
end

unauthenticated do
  root 'site#index'
end

然后在您的用户控制器中,您需要确保检查设计助手current_user而不是id,因为您没有通过。{/ p>

if params[:id]
  @user = User.find params[:id]
else
  @user = current_user
end

如果您不希望用户能够访问此路由,则更容易在控制器中进行管理。只需修改if语句,如

if params[:id]
  if current_user
    @user = current_user
  else
    flash[:notice] = "This page is not available"
    redirect_to root_path
  end
else
  @user = current_user
end

更新路线:

devise_for :users, :path => '', :path_names => { :sign_in => 'login', :sign_out => 'logout',
                                    :password => 'password', :confirmation => 'verification',
                                    :unlock => 'unblock', :registration => 'signup',
                                    :sign_up => 'new' }


get   'login' => 'users/login'

devise_scope :user do
  get   'login',          to: 'devise/sessions#new'
  get   'users/login',    to: 'devise/sessions#new'
  get   'logout',         to: 'devise/sessions#destroy'
  get   'signup',         to: 'devise/registrations#new'
  get   'password',       to: 'devise/passwords#new'
  match 'users/secret',   to: "devise/passwords#create",      via: :post
  match 'sessions/user',  to: 'devise/sessions#create',       via: :post
  match 'users/signup',   to: 'devise/registrations#create',  via: :post
  match 'users/signup',   to: 'devise/registrations#create',  via: :post
end 


#resources :users
resources :sessions

# Authenticated Users:
authenticated :user do
  root to: "users#show", as: :authenticated_root
end 

# Non-Authenticated Users
root to: 'site#index'

get '', to: 'users#show', as: 'user'
get 'edit', to: 'users#edit' as: 'user'