Created
June 16, 2010 13:49
-
-
Save lastk/440710 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateUsers < ActiveRecord::Migration | |
def self.up | |
create_table :users do |t| | |
t.string :name, :null => false | |
t.string :email, :null => false | |
t.string :crypted_password, :null => false | |
t.string :password_salt, :null => false | |
t.string :persistence_token, :null => false | |
t.string :perishable_token, :null => false | |
t.integer :login_count, :null => false, :default => 0 | |
t.integer :failed_login_count, :null => false, :default => 0 | |
t.datetime :last_request_at | |
t.datetime :current_login_at | |
t.datetime :last_login_at | |
t.string :current_login_ip | |
t.string :last_login_ip | |
t.timestamps | |
end | |
end | |
def self.down | |
drop_table :users | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# model: | |
class User < ActiveRecord::Base | |
acts_as_authentic | |
end | |
# outro model: | |
class UserSession < Authlogic::Session::Base | |
end | |
# controller: | |
class UserController < ApplicationController | |
before_filter :require_user, :only => :sign_out | |
def sign_in | |
@user_session = UserSession.new | |
if request.post? | |
@user_session = UserSession.new(params[:user_session]) | |
if @user_session.save | |
redirect_to root_path | |
end | |
end | |
end | |
def sign_out | |
current_user_session.destroy | |
redirect_to root_path | |
end | |
end | |
# depois pra usar nos controllers: | |
before_filter :require_user |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
<% if not current_user %> | |
<%= link_to "Sign in", sign_in_path %> | |
<% end %> | |
--> | |
<% form_for @user_session, :url => sign_in_path do |f| %> | |
<p> | |
<%= f.label :email, "Email:" %><br /> | |
<%= f.text_field :email %> | |
</p> | |
<p> | |
<%= f.label :password, "Password:" %><br /> | |
<%= f.password_field :password %> | |
</p> | |
<p> | |
<%= f.submit "Sign in!" %> | |
</p> | |
<%= f.error_messages %> | |
<% end %> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment