Skip to content

Instantly share code, notes, and snippets.

@tyler-smith
Created September 21, 2012 01:08
Show Gist options
  • Save tyler-smith/3759222 to your computer and use it in GitHub Desktop.
Save tyler-smith/3759222 to your computer and use it in GitHub Desktop.
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
flash[:notice] = 'Dados salvos com sucesso'
redirect_to :action => 'edit'
else
flash[:error] = 'Falha ao salvar dados'
render :edit
end
# Get user data by id
@user = User.find(params[:id])
end
#model
class User < ActiveRecord::Base
attr_accessible :created, :email, :name, :password
before_save :encrypt_password
validates :name, :length => {:in => 3..50}
# Validates email with regex
validates :email, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
# I need pass by here
validates :password, :length => {:in => 3..50}, :if => :has_password?
def has_password?
!@password.blank?
end
def encrypt_password
if password.present?
self.password = BCrypt::Engine.hash_secret(password, BCrypt::Engine.generate_salt)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment