两个用户#show,以及登录后如何重定向到我的用户个人资料

时间:2015-05-28 23:48:00

标签: ruby-on-rails ruby devise

周四大家快乐,我有一个关于路线和重定向的快速问题。我正在进行一项铁路任务,要求我在登录后将路由器重定向到他/她的个人资料。我将如何做到这一点? after_sign_in方法?这是我的路线:

Rails.application.routes.draw do
  get 'users/show'

  get 'users_controller/show'

  devise_for :users
  resources :users

  get 'welcome/index'
  root :to => 'welcome#index'
end

用户控制器:

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

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

设计/会话(登录页面)

<h2>Sign in</h2>

<div class="row">
  <div class="col-md-8">
    <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
      <%= devise_error_messages! %>
      <div class="form-group">
        <%= f.label :email %>
        <%= f.email_field :email, autofocus: true, class: 'form-control', placeholder: "Enter email" %>
      </div>
      <div class="form-group">
        <%= f.label :password %>
        <%= f.password_field :password, class: 'form-control', placeholder: "Enter password" %>
      </div>
      <div class="form-group">
        <% if devise_mapping.rememberable? %>
          <%= f.label :remember_me, class: 'checkbox' do %>
            <%= f.check_box :remember_me %> Remember me
          <% end %>
        <% end %>
        <%= f.submit "Sign in", class: 'btn btn-success' %>
      </div>
      <div class="form-group">
        <%= render "devise/shared/links" %>
      </div>
    <% end %>
  </div>
</div>

感谢您的帮助!也有人可以回答为什么我的佣金路线有两个用户#show?我只知道一个函数(带有user_id的函数)所以我不知道如何创建两个函数。

Rake Routes输出:

rake routes
                  Prefix Verb   URI Pattern                       Controller#Action
              users_show GET    /users/show(.:format)             users#show
   users_controller_show GET    /users_controller/show(.:format)  users_controller#show
        new_user_session GET    /users/sign_in(.:format)          devise/sessions#new
            user_session POST   /users/sign_in(.:format)          devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)         devise/sessions#destroy
           user_password POST   /users/password(.:format)         devise/passwords#create
       new_user_password GET    /users/password/new(.:format)     devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)    devise/passwords#edit
                         PATCH  /users/password(.:format)         devise/passwords#update
                         PUT    /users/password(.:format)         devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)           devise/registrations#cancel
       user_registration POST   /users(.:format)                  devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)          devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)             devise/registrations#edit
                         PATCH  /users(.:format)                  devise/registrations#update
                         PUT    /users(.:format)                  devise/registrations#update
                         DELETE /users(.:format)                  devise/registrations#destroy
       user_confirmation POST   /users/confirmation(.:format)     devise/confirmations#create
   new_user_confirmation GET    /users/confirmation/new(.:format) devise/confirmations#new
                         GET    /users/confirmation(.:format)     devise/confirmations#show
                   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
           welcome_index GET    /welcome/index(.:format)          welcome#index
                    root GET    /       

                      welcome#index

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

默认情况下,Devise会在登录后将用户重定向到root_path。您只需将config / routes.rb中的root设置更改为所需路径,或添加{{1例如

user_root_path

Devise wiki中所述。

关于这两条路线,请注意您的get '/welcome' => "welcome#index", as: :user_root 包含以下两条陈述:

routes.rb

get 'users/show' resources :users 自动添加resources映射到GET /users/:id的路由,这是您想要的。应删除另一条路线以及UsersController#show。有关这方面的更多信息,请参阅Rails Routing Guide

答案 2 :(得分:0)

在应用程序控制器中

  def after_sign_in_path_for(user)
    the link that you want 
  end

在您的示例中

  def after_sign_in_path_for(user)
    user_path(current_user.id)
  end