daily pastebin goal
50%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Ruby - Authenticate.. Login Problems
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5.   <title>Mytimes</title>
  6.   <%= stylesheet_link_tag    "application", :media => "all" %>
  7.   <%= javascript_include_tag "application" %>
  8.   <%= csrf_meta_tags %>
  9. </head>
  10. <body>
  11. <div id="container">
  12.     <div id="header">
  13.         <h1> My Workingtimes </h1>
  14.     </div>
  15.     <div id="content">
  16.         <% if flash[:notice] %>
  17.         <p id="notice">
  18.             <%= flash[:notice] %>
  19.         </p>
  20.         <% end %>
  21.  
  22.         <% if flash[:alert] %>
  23.         <p id= "alert">
  24.             <%= flash[:alert] %>
  25.         </p>
  26.         <% end %>
  27.     </div>
  28.     <div id="footer">
  29.         &copy; 2012 |
  30.         <% if user_signed_in? %>
  31.         <%= link_to "logout", logout_path, method: :delete %>
  32.         <% else %>
  33.         <%= link_to "login", login_path %>
  34.         <% end %>
  35.     </div>
  36. </div>
  37. </body>
  38. </html>
  39.    
  40. Mytimes::Application.routes.draw do
  41.   resources :mytimes
  42.   resources :users, :only => [:new, :create]
  43.   resources :sessions, :only => [:create]
  44.  
  45.   get "login" => "sessions#new", as: "login"
  46.   post "sessions" => "sessions#create", as: "sessions"
  47.   delete "logout" => "sessions#destroy", as: "logout"
  48.    
  49. private
  50.  
  51.   def current_user
  52.     if session[:user_id]
  53.       @current_user ||= User.find(session[:user_id])
  54.     end
  55.   end
  56.  
  57.   def user_signed_in?
  58.     current_user.present?
  59.   end
  60.  
  61.  
  62.  
  63.  
  64.   helper_method :user_signed_in?
  65. end
  66.    
  67. class SessionsController < ApplicationController
  68.   def new
  69. end
  70.  
  71.   def create
  72.     user = User.find_by_email(params[:email])
  73.     if user && user.authenticate(params[:password])
  74.         session[:user_id] = user.id
  75.         redirect_to mytimes_path,
  76.           notice: "Your are signed!"
  77.     else
  78.       flash.now.alert = "Failed Email or Password!"
  79.  
  80.       render "new"
  81.     end
  82. end
  83.  
  84.   def destroy
  85.     session[:user_id] = nil
  86.     redirect_to mytimes_path,
  87.       notice: "You are logged out!"
  88. end
  89.  
  90. end
  91.    
  92. <h2> Log In </h2>
  93.  
  94. <%= form_tag sessions_path do %>
  95.     <p>
  96.         <%= label_tag :email %>
  97.         <%= text_field_tag :email, params[:email] %>
  98.     </p>
  99.     <p>
  100.         <%= label_tag :password %>
  101.         <%= password_field_tag :password %>
  102.     </p>
  103.     <p><%= submit_tag "Log In" %></p>
  104.  
  105. <% end %>
  106.    
  107. <h2>New User</h2>
  108. <%= form_for @user do |f| %>
  109.     <% if @user.errors.any? %>
  110.         <ul>
  111.             <% @user.errors.full_messages.each do |msg| %>
  112.                 <li><%= msg %></li>
  113.             <% end %>
  114.         </ul>
  115.     <% end %>
  116.  
  117. <p>
  118.     <%= f.label :email %>
  119.     <%= f.text_field :email %>
  120. </p>
  121. <p>
  122.     <%= f.label :password %>
  123.     <%= f.password_field :password %>
  124. </p>
  125. <p>
  126.     <%= f.label :password_confirmation %>
  127.     <%= f.password_field :password_confirmation %>
  128. </p>
  129. <p><%= f.submit %></p>
  130. <% end %>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top