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
相关文档:
Ruby和Python的语法比较
其实Ruby和Python非常接近,比大多数别的语言要接近的多,所以喜欢用啥就用啥(大实话,虽然也是废话)。语法上的差别虽然有那么一点,大部分是syntax sugar,我斗胆稍微列几个(python我也忘得差不多了,不对的大家尽管来鞭尸吧),但是主要差异还是设计思想上的:灵活 ......
在Ruby on Rails中真的有一堆Select helper可以用,我们经常容易混淆。常见的有三个..
select, select_tag, collection_select(其余的什么select_date那些不谈)
我们先来看看一个基本的下拉式选项骨架
</p>
<select
name="ROR">
<option
value="1">ROR1</option><br
/>
<optio ......
转自 http://www.javaeye.com/topic/57474
Windows平台的ruby IDE 点评
在MacOS平台几乎没有什么争议性,大家都用TextMate。但是Windows平台可供选择和使用的IDE很多,却各有各的长处和短处。基于我用过的所有ruby IDE点评一下。windows平台的RoR IDE主要分为两类:一类是重量级的全功能IDE,例如Eclipse,Netbeans ......
使用 will_paginate 进行分页和简单查询
在命令行下使用 gem install will_paginate 命令,出现下面结果安装成功
打开 books_controller.rb (你自己的控制器)
注释掉查找全部的方法,使用下面的方法,已经集成根据title进行查询
Ruby代码
#@books = Book.all
@books = Book.pagina ......
ruby常规访问access数据库的方法应该是使用DBI库
:
require 'dbi'
DBI.connect("DBI:ADO:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb;")
可是
简单尝试之后没能成功,提示找不到驱动器ADO,懒得再试,遂找其他方法。
一番搜索之后,发现可以用WIN32OLE来访问access,写一个简单的类包装之:
......