Search code examples
ruby-on-railsrubydevise

Two users#show, and how to redirect to my user profile after login


Happy Thursday everyone, I had a quick question on routes and redirecting. I am working on a rails assignment that asks that I redirect the router to his/her profile after signing in. How would I go about doing that? An after_sign_in method? Here is my routes:

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

Users Controller:

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

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

devise/sessions (Login Page)

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

Thanks for your help! Also could anyone answer why my rake routes has two users#show? I know only one functions (the one with the user_id) so I don't know how two were created.

Rake Routes Output:

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

Solution

  • Define after_sign_in_path_for in ApplicationController. Read this: https://github.com/plataformatec/devise/wiki/How-To%3A-Redirect-to-a-specific-page-on-successful-sign-in-and-sign-out