首先,下载bitnami-rubystack-3.2.1-0-windows-installer.exe
地址:http://bitnami.org/stack/rubystack
简介:BitNamiRubyStack 提供了快速简单的 Ruby on Rails 的一键安装包,包括:Ruby, Subversion, MySQL, SQLite, ImageMagick, 和许多 Ruby Gems, 同时提供可选安装 Apache 2.2 具有URL重写和代理支持,支持 Windows,Linux, and OS X
二、安装:
1、一直按“next”即可完成安装:中间自定义输入六位数据库密码。
默认安装目录:C:\ProgramFiles\BitNami RubyStack
工程(application)默认安装目录:C:\Users\Administrator\BitNamiRubyStack projects\rubystack-3.2.1-0
2、版本:在控制台上输入如下命令可看到版本信息:
Ruby 1.9.3p0
Rails 3.2.1
Ruby Gems 1.8.17
进入数据库命令如下:
三、建立工程:
1、学习资料:
a、网上资料:http://guides.rubyonrails.org/getting_started.html
在线书籍:http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:1.3.1.2
b、Ruby学习资料:ProgrammingRuby中文版 (第二版).pdf
c、rails学习资料:
(i)Rails3中文学习资料(百度文库):http://wenku.baidu.com/view/3b0d2e85b9d528ea81c7794d.html
(ii)Rails3英文学习资料:
[Ruby.on.Rails.3.网络编程视频教程].RubyOnRails3LiveLessons.pdf
(iii)旧版中文rails学习资料:[大家网]Ruby.on.Rails.Web开发之旅[www.TopSage.com].pdf
以上资料均已下载且阅读。
2、步骤:
Ruby实现recaptcha过程:
a、 建立一个工程命名为testApp
在控制台下输入如下命令:
BitNami RubyStack projects>rails new testApp
放在了“C:\Users\Administrator\BitNamiRubyStack projects\testApp"目录下:
打开目录可以看见如下生成的文件及文件夹:
>cd testApp
testApp> rails g scaffold user
testApp> rake db: migrate
(1) 第二种模式:git方式:
https://github.com/southwolf/yinxiangma
gemfile 文件里面添加:gem 'yinxiangma',:git => 'git://github.com/southwolf/yinxiangma.git'
testApp> bundle install
添加代码:1、添加controller验证函数 yinxiangma_valid()&&
2、创建文件:recaptcha.rb
Yinxiangma.configure do |config|
b6ccaa33ebb72559e03e2f6aeb3764bc
config.mode ='live'
end
3、<%=rawyinxiangma_tags %>
testApp>rails s
本地创建及测试gem:1、bundle gem rename_url 或下载gem库(包含lib文件)
gem build rename_url.gemspec
gem 'rename_url', :path => '~/programs/rename_url'或路径‘ C:\yinxiangma0’
bundleinstall
rails s
直接在代码中修改
直接运行rails s
参数说明:
<scripttype="text/javascript"
src="https://api.hinside.cn/index.php?u=hcaptcha&s=module&k=b6ccaa33ebb72559e03e2f6aeb3764bc">
</script>
include("lib/hcaptcha/hcaptcha.lib.php");
$_HCaptchaPrivateKey="a9ebfa885ed74e9d5cc5e3e2bc681b68";
$HCaptchaObj= new HCaptcha($_HCaptchaVerifyServer, $_HCaptchaVerifyServerPort,$_HCaptchaPrivateKey);
$HCaptchaVerifyResult= $HCaptchaObj->Verify($_HCaptchaVerifyURL,isset($_POST['HCaptchaRemoteAddr']) ? $_POST['HCaptchaRemoteAddr'] :$_SERVER['REMOTE_ADDR'], isset($_POST['HCaptchaInput']) ?
https://api.hinside.cn/index.php?&u=hcaptcha&s=home&k=&sid=
https://api.hinside.cn/default.php?&u=hcaptcha&s=verify&k=&c=&ip=&sid=
Content= _
"u=hcaptcha&s=verify&k=" & HcaptchaPrivatekey& _
&ip=" & HCaptchaRemoteAddr & _
&sid="& HcaptchaSid & _
&c="& HCaptchaInput
2、文件上传到github网:这里如果要上传代码,有几个关键步骤:
git init
git init
这个命令会在当前目录下创建一个.git文件夹。
git add .
git add .
这个命令会把当前路径下的所有文件,添加到待上传的文件列表中。
git commit -m "xxxxx" git commit -m "xxxxx"
git remote add origin git@github.com:zspoplar/yinxiangma1.git git remote add origingit@github.com:xuhongxin/deom.git
git push -u origin master
修改代码:
$ git add README.rdoc
$ git commit -m "delete add ttt and next"
$ git push –u origin master
公司的密钥:
ENV['RECAPTCHA_PUBLIC_KEY'] = 'b6ccaa33ebb72559e03e2f6aeb3764bc'
ENV['RECAPTCHA_PRIVATE_KEY'] = 'a9ebfa885ed74e9d5cc5e3e2bc681b68'
||谷歌recaptcha:
https://github.com/ambethia/recaptcha
前面相同: https://github.com/dayne/ruby-recaptcha-git
Ruby中文手册:http://guides.ruby.tw/rails3/getting_started.html
添加代码:
1、 添加文件config/initializers/recaptcha.rb
Recaptcha.configure do |config|
config.public_key = 'b6ccaa33ebb72559e03e2f6aeb3764bc'
config.private_key= 'a9ebfa885ed74e9d5cc5e3e2bc681b68'
end
Recaptcha.configuredo |config|
config.public_key = '6LfFvM0SAAAAAPMePoKv3OoGAirYtxSQHz1ycWjw '
config.private_key = '6LfFvM0SAAAAAKcdjEZfLObXZBzUCUUinCz_RKaY '
config.proxy = 'http://myrpoxy.com.au:8080'
end
require'ruby-recaptcha'
RCC_PUB ='6LfFvM0SAAAAAPMePoKv3OoGAirYtxSQHz1ycWjw'
RCC_PRIV ='6LfFvM0SAAAAAKcdjEZfLObXZBzUCUUinCz_RKaY'
2、验证:controller.rb
respond_to do |format|
if verify_recaptcha(:model => @post(修改自己的文件名) => "Oh! It's error with reCAPTCHA!") && @post(修改自己的文件名).save
# ...
else
# ...
end
end
3、view
ENV['RECAPTCHA_PUBLIC_KEY'] = ' 6LfFvM0SAAAAAPMePoKv3OoGAirYtxSQHz1ycWjw '
ENV['RECAPTCHA_PRIVATE_KEY'] = '6LfFvM0SAAAAAKcdjEZfLObXZBzUCUUinCz_RKaY '
<%= raw recaptcha_tags %>
recaptcha_tags :public_key => '6LfFvM0SAAAAAPMePoKv3OoGAirYtxSQHz1ycWjw '
verify_recaptcha :private_key => '6LfFvM0SAAAAAKcdjEZfLObXZBzUCUUinCz_RKaY '
(2)第一种模式:plugin方式:
b、远程下载插件(关键、难点):
在控制台下输入如下命令:
BitNamiRubyStack projects>cd testApp
BitNamiRubyStack projects \testApp>rails plugin install
https://github.com/tsevan/ruby-recaptcha.git
testApp/gemfile里添加:
BitNami RubyStack projects\testApp >bundle install
注明:插件的下载是本次实现过程的关键和难点,尝试过多种方法(包括更换命令,文件路径,软件环境。。。等等)。
最后在文件夹:
"C:\Users\Administrator\BitNamiRubyStack projects\testApp\vendor\plugin"
找到下载的recaptcha插件。
c、文件夹里添加代码(重点):
具体添加过程如下官网有介绍:
https://bitbucket.org/mml/ruby-recaptcha/wiki/Home
(i) testApp\config\environment.rb文件里添加如下代码:
其中:是从官网http://www.google.com/recaptcha申请得到的公钥和私钥.
(ii)testApp\app\controllers\application_controller.rb文件里添加如下代码:
(iii)testApp\app\controllers\users_controller.rb文件里添加如下代码:
注明:以上代码是通过找各种资料,自己添加上去的,官网没有完全提供.这段代码实现验证功能。
(iiii)testApp\app\helpers\application_helper.rb文件里添加如下代码:
(iiiii)testApp\app\views\users\_form.html.erb文件里添加代码(显示页面):
注明:<%=raw get_recaptcha %>中的raw实现是转义的功能。
相关介绍可以看如下资料:http://railscasts.com/episodes/204-xss-protection-in-rails-3?autoplay=true(英文视频)
(iiiiii)testApp\config\environments\test.rb文件中添加代码:
d、完成验证:
添加好代码以后,在控制台下输入如下命令:
BitNami RubyStack projects\testApp>rails s
打开页面:http://localhost:3000/ 出现如下界面:
打开http://localhost:3000/users/new 出现如下界面:
输入验证码(错误):
单击“summit”按钮,出现如下界面并实现了验证功能:
再次输入验证码(正确):
出现如下界面: