RUBYʵ¼ù—´øÃÜÂë¼ÓÃܵÄÓû§´´½¨¼°ÐÞ¸Ä
¿ª·¢»·¾³
Ruby: Ruby1.9.1
Rails: Rails2.3.5
Mysql:Mysql5.0.9
Driver:mysql-2.8.1-x86-mingw32.gem
IDE:Rubymine2.0.1
Ò»¡¢´´½¨Êý¾Ý±íUsers
ÀûÓÃRubyMine×Ô´øµÄScaffold¹¤¾ß´´½¨Êý¾Ý±íUsers£¬Ò²¿ÉÒÔÊÖ¶¯´´½¨
¶þ¡¢´´½¨ControllerºÍView
RubyÏîÄ¿—>ÓÒ¼ü—>Create Model
Íê³Éºó½«×Ô¶¯Éú³ÉÏàÓ¦µÄÎļþ
Èý¡¢ÐÞ¸ÄModel user.rb
ÀûÓÃDigest/SHA1¶ÔÃÜÂë½øÐмÓÃÜ£¬ÊµÏÖ¼ÓÃܱ£´æ
Ð޸ĺó´úÂëÈçÏ£º
require "digest/sha1"
class User < ActiveRecord::Base
attr_accessor :hashed_password,:repassword
attr_accessible :username, :hashed_password, :repassword
validates_uniqueness_of :username
validates_presence_of :username, :hashed_password
def before_create
self.password = User.hash_password(self.hashed_password)
end
def after_create
@hashed_password = nil
end
def before_update
self.password = User.hash_password(self.hashed_password)
end
def after_update
@hashed_password = nil
end
private
def self.hash_password(hashed_password)
Digest::SHA1.hexdigest(hashed_password)
end
end
ËÄ¡¢ÐÞ¸Äusers_controller.rb
ÐÞ¸Äupdate·½·¨£¬ÊµÏÖµ±½øÐÐedit²Ù×÷ʱÏÈÅжÏpasswordÓë password_confirmÊÇ·ñÒ»Ö£¬
Èç¹ûÒ»Ö£¬Ôò½øÐÐupdate²Ù×÷£¬·ñÔòÌáʾÓû§password Óë password_confirm ÊäÈë²»Ò»ÖÂ
Ð޸ĺó´úÂëÈçÏ£º
def update
@user = User.find(params[:id])
respond_to do |format|
print "user: #{params[:user]}"
if params[:user]["hashed_password"] == params[:user]["repassword"]
if @user.update_attributes(params[:user])
flash[:notice] = 'User was successfully updated.'
format.html { redirect_to(@user) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
else
flash[:notice] = 'Password and Password confirm are not the same'
format.html { render :action => "edit" }
format.xml
Ïà¹ØÎĵµ£º
selfÉÏÏÂÎÄ
RubyµÄselfÓкÍJavaµÄthisÏàËÆÖ®´¦£¬µ«ÓÖ´ó²»Ïàͬ¡£JavaµÄ·½·¨¶¼ÊÇÔÚʵÀý·½·¨ÖÐÒýÓã¬ËùÒÔthisÒ»°ã¶¼ÊÇÖ¸Ïòµ±Ç°¶ÔÏóµÄ¡£¶øRubyµÄ´úÂëÖðÐÐÖ´ÐУ¬ËùÒÔÔÚ²»Í¬µÄÉÏÏÂÎÄ(context)self¾ÍÓÐÁ˲»Í¬µÄº¬Ò壬ÏÈÀ´¿´¿´³£¼ûµÄcontext self¶¼´ú±íÄÄЩ
1
2
3
4
5
6
7
8
9
10
11
12
13
......
ÕâÊÇÒ»¸ö¿ªÔ´£¬Ãâ·Ñ£¬ÁéÇÉ£¬¼òÒ×µÄweb×Ô¶¯»¯²âÊÔ×éºÏ¿ò¼Ü£»Í¨³£Çé¿ö£¬¿ªÔ´µÄ¶«Î÷×ÜÊǸøÈËÒÔÃÔÈË£¬¿ª·ÅʽµÄ´¥¸Ð£¬µ«Í¬Ê±£¬Ò²ÈÃÈËÄÑÒÔ¿ìËÙ½Ó½ü£¨ÏñÊǶ«·½ÄÐÈË¿´´ýÎ÷·½ÃÀÅ®£©£»ÒªÁ˽âÒ»¸ö¿ªÔ´¶øÇÒ×éºÏƯÁÁµÄ¿ò¼Ü£¬×ÜÊÇÒª·ÑЩÖÜÕÂ......×ܶøÑÔÖ®£¬Ëü²»»áÏñÊշѵĶ«Î÷£¬ÄãºÜÌÖÑáËü£¬µ«²»¿É·ñÈÏ£¬ÄãµÄ¸¶·ÑÆäʵ»»È¡Á˱ðÈ˵ÄËöËéÀͶ ......
ÎÒÃÇÖªµÀrubyÖжÔÓÚÕûÊýµÄ[],[]=,<<,>>²Ù×÷ÊÇÕë¶ÔÓÚ¶þ½øÖƵÄÖµÀ´ÔËËãµÄ¡£
ÎÒÏÖÔÚдһ¸öÕë¶ÔÊ®½øÖÆÊý²Ù×÷µÄÀ࣬ӵÓÐÕûÊýµÄËùÓз½·¨£¬ÈçÏÂ:
class InterEx
def initialize(val=0)
@val=val
end
def to_s
@val.to_s
end
def [](idx)
self.to_s[idx].to_i
end
d ......
ruby³£¹æ·ÃÎÊaccessÊý¾Ý¿âµÄ·½·¨Ó¦¸ÃÊÇʹÓÃDBI¿â
£º
require 'dbi'
DBI.connect("DBI:ADO:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb;")
¿ÉÊÇ
¼òµ¥³¢ÊÔÖ®ºóûÄܳɹ¦£¬ÌáʾÕÒ²»µ½Çý¶¯Æ÷ADO,ÀÁµÃÔÙÊÔ£¬ËìÕÒÆäËû·½·¨¡£
Ò»·¬ËÑË÷Ö®ºó£¬·¢ÏÖ¿ÉÒÔÓÃWIN32OLEÀ´·ÃÎÊaccess,дһ¸ö¼òµ¥µÄÀà°ü×°Ö®:
......