自己写的rails登录界面

阅读更多
rails g controller users new
rails g model user email:string password_hash:string password_salt:string
rake db:migrate
rails dbconsole
rails g controller sessions new
Gemfile
gem "bcrypt-ruby", :require => "bcrypt"
models/user.rb
class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation
  
  attr_accessor :password
  before_save :encrypt_password
  
  validates_confirmation_of :password
  validates_presence_of :password, :on => :create
  validates_presence_of :email
  validates_uniqueness_of :email
  
  def self.authenticate(email, password)
    user = find_by_email(email)
    if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
      user
    else
      nil
    end
  end
  
  def encrypt_password
    if password.present?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end
end
users_controller.rb
def new
  @user = User.new
end

def create
  @user = User.new(params[:user])
  if @user.save
    redirect_to root_url, :notice => "Signed up!"
  else
    render "new"
  end
end
sessions_controller.rb
def new
end

def create
  user = User.authenticate(params[:email], params[:password])
  if user
    session[:user_id] = user.id
    redirect_to root_url, :notice => "Logged in!"
  else
    flash.now.alert = "Invalid email or password"
    render "new"
  end
end

def destroy
  session[:user_id] = nil
  redirect_to root_url, :notice => "Logged out!"
end
application_controller.rb
helper_method :current_user

private

def current_user
  @current_user ||= User.find(session[:user_id]) if session[:user_id]
end
routes.rb
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
root :to => "users#new"
resources :users
resources :sessions
users/new.html.erb

Sign Up

<%= form_for @user do |f| %> <% if @user.errors.any? %> class="error_messages">

Form is invalid

    <% for message in @user.errors.full_messages %>
  • <%= message %>
  • <% end %>
<% end %>

<%= f.label :email %> /> <%= f.text_field :email %>

<%= f.label :password %> /> <%= f.password_field :password %>

<%= f.label :password_confirmation %> /> <%= f.password_field :password_confirmation %>

class="button"><%= f.submit %>

<% end %>
sessions/new.html.erb

Log in

<%= form_tag sessions_path do %>

<%= label_tag :email %> /> <%= text_field_tag :email, params[:email] %>

<%= label_tag :password %> /> <%= password_field_tag :password %>

class="button"><%= submit_tag "Log in" %>

<% end %>
layouts/application.html.erb
 id="user_nav">
  <% if current_user %>
    Logged in as <%= current_user.email %>.
    <%= link_to "Log out", log_out_path %>
  <% else %>
    <%= link_to "Sign up", sign_up_path %> or
    <%= link_to "log in", log_in_path %>
  <% end %>
<% flash.each do |name, msg| %> <%= content_tag :div, msg, :id => "flash_#{name}" %> <% end %>

你可能感兴趣的:(自己写的rails登录界面)