在设计登录时,用户未被重定向到用户显示页面

时间:2013-11-03 20:16:04

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

登录或注册后,应将用户重定向到其各自的展示页面。我正在使用Hartl指南中的一些代码,但我似乎无法让它工作:

控制器:

class UsersController < ApplicationController
    before_filter :authenticate_user!

  def show
    @user = User.find(params[:id])
  end

  def new
  end

  def create
    @user = User.new(user_params)
    if @user.save
        flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      redirect_to login_path
    end
  end

private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
    end

end

路线:

  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   "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
  end

  resources :users
  resources :sessions    

  root 'home#index'

我的/views/users/show.html.erb只是一个简单的页面,直到我可以让重定向工作。

编辑:

rake routes

                  Prefix Verb   URI Pattern                  Controller#Action
        new_user_session GET    /login(.:format)             devise/sessions#new
            user_session POST   /login(.:format)             devise/sessions#create
    destroy_user_session DELETE /logout(.:format)            devise/sessions#destroy
           user_password POST   /password(.:format)          devise/passwords#create
       new_user_password GET    /password/new(.:format)      devise/passwords#new
      edit_user_password GET    /password/edit(.:format)     devise/passwords#edit
                         PATCH  /password(.:format)          devise/passwords#update
                         PUT    /password(.:format)          devise/passwords#update
cancel_user_registration GET    /signup/cancel(.:format)     devise/registrations#cancel
       user_registration POST   /signup(.:format)            devise/registrations#create
   new_user_registration GET    /signup/new(.:format)        devise/registrations#new
  edit_user_registration GET    /signup/edit(.:format)       devise/registrations#edit
                         PATCH  /signup(.:format)            devise/registrations#update
                         PUT    /signup(.:format)            devise/registrations#update
                         DELETE /signup(.:format)            devise/registrations#destroy
                   login GET    /login(.:format)             devise/sessions#new
                  logout GET    /logout(.:format)            devise/sessions#destroy
                  signup GET    /signup(.:format)            devise/registrations#new
                password GET    /password(.:format)          devise/passwords#new
            users_secret POST   /users/secret(.:format)      devise/passwords#create
           sessions_user POST   /sessions/user(.:format)     devise/sessions#create
            users_signup POST   /users/signup(.:format)      devise/registrations#create
                   users GET    /users(.:format)             users#index
                         POST   /users(.:format)             users#create
                new_user GET    /users/new(.:format)         users#new
               edit_user GET    /users/:id/edit(.:format)    users#edit
                    user GET    /users/:id(.:format)         users#show
                         PATCH  /users/:id(.:format)         users#update
                         PUT    /users/:id(.:format)         users#update
                         DELETE /users/:id(.:format)         users#destroy
                sessions GET    /sessions(.:format)          sessions#index
                         POST   /sessions(.:format)          sessions#create
             new_session GET    /sessions/new(.:format)      sessions#new
            edit_session GET    /sessions/:id/edit(.:format) sessions#edit
                 session GET    /sessions/:id(.:format)      sessions#show
                         PATCH  /sessions/:id(.:format)      sessions#update
                         PUT    /sessions/:id(.:format)      sessions#update
                         DELETE /sessions/:id(.:format)      sessions#destroy
                    root GET    /                            home#index

1 个答案:

答案 0 :(得分:0)

问题出在您的佣金路线输出的最后一行:

  root GET    /                            home#index

指向routes.rb

中的这一行
 root 'home#index'

您应该删除routes.rb中的该行,并将其替换为您想要的根路径。