To Projects

Implementing a Login Page for a Website

A Tutorial

Create a sample existing website to which a validation login page will be added later.

  1. Create a Rails project named GradeRecords.
     
  2. Create a scaffold named Grade:
    Run the rake command.
     
  3. Copy the following code into db/seed.rb: grade-data.rb. Wait to run rake db:seed until later.
     

Directions for adding a validation login page:

  1. Create a model named LoginInfo:
    Run the rake command.
     
  2. Populate the LoginInfos table with usernames and encrypted passwords with this file: passwords.rb. Copy and paste this code into the db/seeds.rb file. Then enter

     
  3. Create a new controller:
  4. Copy the code from these links: login_controller.rb, login-page view, login_page.html.erb, layout login.html.erb, and CSS file login.css.
     
  5. Add the following code to the methods index, show, new, edit, and destroy in the file grades_controller.rb:
      if !session[:logged_in]
        redirect_to :controller => 'login', 
                    :action => 'login_page' 
        return
      end
      
    For the index, show, new, and destroy methods, place this code directly before the line
      respond_to do |format|
      
    In the edit method, place it at the end of the method, directly before the final end statement
     
  6. Change the name of the layout file apps/views/layouts.application.html.erb to grades.html.erb. Place this code at the bottom of this file right before the end body tag:
      <hr />
      
      <%= form_tag :controller => 'login', 
                   :action => 'logout' do %>
      <%= submit_tag 'Logout', :class => 'ctrl' %>
      <% end %>
      
  7. Add these routes to config/routes.rb:
      post "login/login_page"
      post "login/logout"