Skip to content

Instantly share code, notes, and snippets.

@agaelebe
Created September 25, 2012 01:36
Show Gist options
  • Save agaelebe/3779484 to your computer and use it in GitHub Desktop.
Save agaelebe/3779484 to your computer and use it in GitHub Desktop.
# Controller
module Admin
class UsersController < AdminController
def edit
@user = User.find(params[:id])
end
def update
if request.put?
@user = User.find(params[:id])
if @user.update_attributes(to_update)
session[:logged_user] = @logged_user = @user
flash[:notice] = 'Dados salvos com sucesso'
redirect_to :action => 'edit'
else
flash[:error] = 'Falha ao salvar dados'
render :edit
end
end
end
end
end
# Model
class User < ActiveRecord::Base
attr_accessible :created, :email, :name, :password
before_save :encrypt_password
validates :name, :length => {:in => 3..50}
validates :email, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
validates :password, :length => {:in => 3..50}, :if => :has_password?
def has_password?
!password.blank?
end
def encrypt_password
if password.present? && !password.blank?
password = BCrypt::Password.create(password)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment