蕲春人 - 软件开发

简单就是美.

Set Proxy in Server Side to Get Crossing Domain Ajax Request

proxy_controller.rb
1
2
3
4
5
6
7
8
9
10
11
class ProxyController < ApplicationController

  # GET /proxy/:url
  def get_handle
    require 'open-uri'
    file = open(params[:url])
    contents = file.read
    render :text => contents
  end

end
in config/routes.rb
1
get "/proxy" => "proxy#get_handle"
javascript useage
1
$.get("/proxy?url=" + remote_url, function(data){

Install Mongo on Mac

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
caojinhuamatoMacBook-Pro:code caojinhua$ brew install mongodb
==> Downloading http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-1.8.1.tgz
######################################################################## 100.0%
==> Caveats
If this is your first install, automatically load on login with:
    mkdir -p ~/Library/LaunchAgents
    cp /usr/local/Cellar/mongodb/1.8.1-x86_64/org.mongodb.mongod.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/org.mongodb.mongod.plist

If this is an upgrade and you already have the org.mongodb.mongod.plist loaded:
    launchctl unload -w ~/Library/LaunchAgents/org.mongodb.mongod.plist
    cp /usr/local/Cellar/mongodb/1.8.1-x86_64/org.mongodb.mongod.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/org.mongodb.mongod.plist

Or start it manually:
    mongod run --config /usr/local/Cellar/mongodb/1.8.1-x86_64/mongod.conf
MongoDB 1.8+ includes a feature for Write Ahead Logging (Journaling), which has been enabled by default.
This is not the default in production (Journaling is disabled); to disable journaling, use --nojournal.
==> Summary
/usr/local/Cellar/mongodb/1.8.1-x86_64: 16 files, 93M, built in 2 seconds

去掉textmate源代码中的隐形空格

有这个需要,主要是因为不想在git提交后的diff中无看到不有意思的diff显示。使用textmate的同学可以用这个工具来解决这个小问题。

安装方法

install step
1
2
3
4
5
cd ~/Library/Application\ Support/TextMate/Bundles/
git clone git://github.com/glennr/uber-glory-tmbundle.git Uber\ Glory.tmbundle
cd Uber\ Glory.tmbundle
git submodule update --init
osascript -e 'tell app "TextMate" to reload bundles'

使用Unicorn

Install

1
gem install unicorn

Then add gem ‘unicorn’ to Gemfile. bundle exec unicorn_rails to start rails app at 8080 port.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
caojinhua:qichunren.github.com caojinhua$ unicorn_rails --help
Usage: unicorn_rails [ruby options] [unicorn_rails options] [rackup config file]
Ruby options:
  -e, --eval LINE          evaluate a LINE of code
  -d, --debug              set debugging flags (set $DEBUG to true)
  -w, --warn               turn warnings on for your script
  -I, --include PATH       specify $LOAD_PATH (may be used more than once)
  -r, --require LIBRARY    require the library, before executing your script
unicorn_rails options:
  -o, --host HOST          listen on HOST (default: 0.0.0.0)
  -p, --port PORT          use PORT (default: 8080)
  -E, --env RAILS_ENV      use RAILS_ENV for defaults (default: development)
  -D, --daemonize          run daemonized in the background
  -l {HOST:PORT|PATH},     listen on HOST:PORT or PATH
      --listen             this may be specified multiple times
                           (default: 0.0.0.0:8080)
  -c, --config-file FILE   Unicorn-specific config file

      --path PATH          Runs Rails app mounted at a specific path.
                           (default: /)
Common options:
  -h, --help               Show this message
  -v, --version            Show version

This is a smaple unicorn config file:

config/unicorn.conf.rb
1
2
3
4
5
6
7
worker_processes 2
working_directory "/www/temp/ntmenu2/current"
listen 3000, :tcp_nopush => true
timeout 30
pid "/www/temp/ntmenu2/current/tmp/pids/unicorn.pid"
stderr_path "/www/temp/unicorn.stderr.log"
stdout_path "/www/temp/unicorn.stdout.log"

使用Juggernaut

Juggernaut是基于Node.js的一个实时(Realtime)Web的解决方案。使用起来很方便。

安装方法

  1. 安装Node.js: brew install node
  2. 安装Redis: brew install redis
  3. 安装NPM: curl http://npmjs.org/install.sh | sh
  4. 安装Juggernaut: 这个会把Juggernaut安装到当前目录,所以我应该先进行项目的/vendor/third目录,然后执行 npm install juggernaut
1
2
3
4
5
6
usermatoMacBook-Pro:third qichunren$ npm install juggernaut
redis@0.5.11 ./node_modules/juggernaut/node_modules/redis
node-static-maccman@0.5.3 ./node_modules/juggernaut/node_modules/node-static-maccman
socket.io@0.6.17 ./node_modules/juggernaut/node_modules/socket.io
juggernaut@2.0.4 ./node_modules/juggernaut
usermatoMacBook-Pro:third qichunren$  
  1. 安装Juggernaut gem:
1
gem install juggernaut

使用方法

我们在自己的项目中只需要引入http://localhost:8080/application.js 这个js文件即可。 然后在页面中可以这样接收服务器端的消息:

1
2
3
4
5
6
<script type="text/javascript" charset="utf-8">
  var jug = new Juggernaut;
  jug.subscribe("channel1", function(data){
    console.log("Got data: " + data);
  });
</script>

服务器端直接发消息:

1
2
require "juggernaut"
Juggernaut.publish("channel1", "Some data")

另外在启动了Juggernaut后,它默认在8080端口上有一个Helloword的应用,可以了解一下。

在Mac上使用gitX

gitX是mac上一个git的可视化工具,可以与gravatar\gist集成在一起。我比较喜欢它的界面形式,感觉很舒服。

安装方法:

最简单的安装方法就是下载它的二进制包,直接运行。 在它的wiki页面找到二进制包的下载地址,下载后,解压,直接运行,程序界面就出来了。

然后点菜单“GitX”中的’Enable Terminal Usage …’, 这样可以直接在命令行窗口中,在项目的根目录中直接运行gitx来启动gitX了。

My current project

更新

gitX官方的这个版本已经好久没有更新了,另外它也有一些缺点,如不能方便从文件中复制代码(很难选中代码的),我找到这个版本,它从原官方的版本更流行。另外它真的很好用,比官方的版本提高了很多功能点。你自己慢慢体会吧。

学习HTML5杂记

什么是 HTML5?

HTML5 将成为 HTML、XHTML 以及 HTML DOM 的新标准。 HTML 的上一个版本诞生于 1999 年。自从那以后,Web 世界已经经历了巨变。 HTML5 仍处于完善之中。然而,大部分现代浏览器已经具备了某些 HTML5 支持。

新特性

HTML5 中的一些有趣的新特性: + 用于绘画的 canvas 元素 + 用于媒介回放的 video 和 audio 元素 + 对本地离线存储的更好的支持 + 新的特殊内容元素,比如 article、footer、header、nav、section + 新的表单控件,比如 calendar、date、time、email、url、search

Video标签

参考资料

    <!DOCTYPE HTML>
<html>
<body>

<video width="320" height="240" controls="controls">
  <source src="movie.ogg" type="video/ogg">
  <source src="movie.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>

</body>
</html>
  

Audio标签

参考资料

    <!DOCTYPE HTML>
<html>
<body>

<audio controls="controls">
  <source src="song.ogg" type="audio/ogg">
  <source src="song.mp3" type="audio/mpeg">
Your browser does not support the audio tag.
</audio> 

</body>
</html>
  

Canvas元素

参考资料

HTML5 Web存储

参考资料

标签

属性

事件

Resources

将Rspec的测试任务添加到Rakefile中

写好Rspec测试好,一般是通过rspec spec/xxx_spec.rb这样来进行测试,这样不能批量进行多个spec文件的测试,解决方法是将rspec的测试任务添加了项目根目录中的Rakefile文件中去,以后直接执行rake或者rake spec就直接测试所有用例了。

1
2
3
4
5
6
7
8
9
10
require "rubygems"
require 'rake'
require 'rspec/core/rake_task'

task :default => :spec

RSpec::Core::RakeTask.new(:spec) do |t|
  t.pattern = Dir.glob('spec/**/*_spec.rb')
  t.rspec_opts = '--format progress -c'
end

Resources

初试Rails 3.1 Beta

看到消息 说Rails3.1 Beta出来了,我立刻就想试试,先从大体上了解一下看做了哪些改变。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
usermatoMacBook-Pro:code qichunren$ rvm use 1.9.2
Using /Users/qichunren/.rvm/gems/ruby-1.9.2-p0
usermatoMacBook-Pro:jjsc_web_backend qichunren$ gem install rails --pre
Successfully installed multi_json-1.0.1
Successfully installed activesupport-3.1.0.beta1
Successfully installed i18n-0.6.0beta1
Successfully installed activemodel-3.1.0.beta1
Successfully installed rack-1.3.0.beta
Successfully installed rack-cache-1.0.1
Successfully installed rack-test-0.6.0
Successfully installed rack-mount-0.7.2
Successfully installed hike-1.0.0
Successfully installed sprockets-2.0.0.beta.2
Successfully installed tzinfo-0.3.27
Successfully installed erubis-2.7.0
Successfully installed actionpack-3.1.0.beta1
Successfully installed arel-2.1.0
Successfully installed activerecord-3.1.0.beta1
Successfully installed activeresource-3.1.0.beta1
Successfully installed mail-2.3.0
Successfully installed actionmailer-3.1.0.beta1
Successfully installed rack-ssl-1.3.2
Successfully installed railties-3.1.0.beta1
Successfully installed rails-3.1.0.beta1
21 gems installed
Installing ri documentation for multi_json-1.0.1...
Building YARD (yri) index for multi_json-1.0.1...
Installing ri documentation for activesupport-3.1.0.beta1...
Building YARD (yri) index for activesupport-3.1.0.beta1...
Installing ri documentation for i18n-0.6.0beta1...
Building YARD (yri) index for i18n-0.6.0beta1...
Installing ri documentation for activemodel-3.1.0.beta1...
Building YARD (yri) index for activemodel-3.1.0.beta1...
Installing ri documentation for rack-1.3.0.beta...
Building YARD (yri) index for rack-1.3.0.beta...
Installing ri documentation for rack-cache-1.0.1...
Building YARD (yri) index for rack-cache-1.0.1...
Installing ri documentation for rack-test-0.6.0...
Building YARD (yri) index for rack-test-0.6.0...
Installing ri documentation for rack-mount-0.7.2...
Building YARD (yri) index for rack-mount-0.7.2...
Installing ri documentation for hike-1.0.0...
Building YARD (yri) index for hike-1.0.0...
Installing ri documentation for sprockets-2.0.0.beta.2...
Building YARD (yri) index for sprockets-2.0.0.beta.2...
Installing ri documentation for tzinfo-0.3.27...
Building YARD (yri) index for tzinfo-0.3.27...
Installing ri documentation for erubis-2.7.0...
Building YARD (yri) index for erubis-2.7.0...
Installing ri documentation for actionpack-3.1.0.beta1...
Building YARD (yri) index for actionpack-3.1.0.beta1...
Building YARD (yri) index for arel-2.1.0...
Installing ri documentation for activerecord-3.1.0.beta1...
Building YARD (yri) index for activerecord-3.1.0.beta1...
Installing ri documentation for activeresource-3.1.0.beta1...
Building YARD (yri) index for activeresource-3.1.0.beta1...
Installing ri documentation for mail-2.3.0...
Building YARD (yri) index for mail-2.3.0...
Installing ri documentation for actionmailer-3.1.0.beta1...
Building YARD (yri) index for actionmailer-3.1.0.beta1...
Installing ri documentation for rack-ssl-1.3.2...
Building YARD (yri) index for rack-ssl-1.3.2...
Installing ri documentation for railties-3.1.0.beta1...
Building YARD (yri) index for railties-3.1.0.beta1...
Installing ri documentation for rails-3.1.0.beta1...
file 'lib' not found
Building YARD (yri) index for rails-3.1.0.beta1...
Installing RDoc documentation for multi_json-1.0.1...
Installing RDoc documentation for activesupport-3.1.0.beta1...
Installing RDoc documentation for i18n-0.6.0beta1...
Installing RDoc documentation for activemodel-3.1.0.beta1...
Installing RDoc documentation for rack-1.3.0.beta...
Installing RDoc documentation for rack-cache-1.0.1...
Installing RDoc documentation for rack-test-0.6.0...
Installing RDoc documentation for rack-mount-0.7.2...
Installing RDoc documentation for hike-1.0.0...
Installing RDoc documentation for sprockets-2.0.0.beta.2...
Installing RDoc documentation for tzinfo-0.3.27...
Installing RDoc documentation for erubis-2.7.0...
Installing RDoc documentation for actionpack-3.1.0.beta1...
Installing RDoc documentation for activerecord-3.1.0.beta1...
Installing RDoc documentation for activeresource-3.1.0.beta1...
Installing RDoc documentation for mail-2.3.0...
Installing RDoc documentation for actionmailer-3.1.0.beta1...
Installing RDoc documentation for rack-ssl-1.3.2...
Installing RDoc documentation for railties-3.1.0.beta1...
Installing RDoc documentation for rails-3.1.0.beta1...
file 'lib' not found  
usermatoMacBook-Pro:code qichunren$ rails -v
Rails 3.1.0.beta1

看看rails命令有没有一些改动:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
usermatoMacBook-Pro:code qichunren$ rails --help
Usage:
  rails new APP_PATH [options]

Options:
  -r, [--ruby=PATH]              # Path to the Ruby binary of your choice
                                 # Default: /Users/qichunren/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
  -b, [--builder=BUILDER]        # Path to a application builder (can be a filesystem path or URL)
  -m, [--template=TEMPLATE]      # Path to an application template (can be a filesystem path or URL)
      [--skip-gemfile]           # Don't create a Gemfile
  -G, [--skip-git]               # Skip Git ignores and keeps
  -O, [--skip-active-record]     # Skip Active Record files
  -d, [--database=DATABASE]      # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db/jdbcmysql/jdbcsqlite3/jdbcpostgresql)
                                 # Default: sqlite3
  -j, [--javascript=JAVASCRIPT]  # Preconfigure for selected JavaScript library
                                 # Default: jquery
  -J, [--skip-javascript]        # Skip JavaScript files
      [--dev]                    # Setup the application with Gemfile pointing to your Rails checkout
      [--edge]                   # Setup the application with Gemfile pointing to Rails repository
  -T, [--skip-test-unit]         # Skip Test::Unit files
      [--old-style-hash]         # Force using old style hash (:foo => 'bar') on Ruby >= 1.9

Runtime options:
  -f, [--force]    # Overwrite files that already exist
  -p, [--pretend]  # Run but do not make any changes
  -q, [--quiet]    # Supress status output
  -s, [--skip]     # Skip files that already exist

Rails options:
  -h, [--help]     # Show this help message and quit
  -v, [--version]  # Show Rails version number and quit

Description:
    The 'rails new' command creates a new Rails application with a default
    directory structure and configuration at the path you specify.

Example:
    rails new ~/Code/Ruby/weblog

    This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
    See the README in the newly created application to get going.
usermatoMacBook-Pro:code qichunren$

继续建立一个新项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
usermatoMacBook-Pro:code qichunren$ rails new rails3
      create  
      create  README
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/images/rails.png
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/mailers
      create  app/models
      create  app/views/layouts/application.html.erb
      create  app/mailers/.gitkeep
      create  app/models/.gitkeep
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  doc
      create  doc/README_FOR_APP
      create  lib
      create  lib/tasks
      create  lib/tasks/.gitkeep
      create  log
      create  log/.gitkeep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/index.html
      create  public/robots.txt
      create  script
      create  script/rails
      create  test/fixtures
      create  test/fixtures/.gitkeep
      create  test/functional
      create  test/functional/.gitkeep
      create  test/integration
      create  test/integration/.gitkeep
      create  test/unit
      create  test/unit/.gitkeep
      create  test/performance/browsing_test.rb
      create  test/test_helper.rb
      create  tmp/cache
      create  tmp/cache/.gitkeep
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.gitkeep
      create  vendor/plugins
      create  vendor/plugins/.gitkeep
usermatoMacBook-Pro:code qichunren$ 

创建一个scaffold user后,发现在页面里/assets/application.js中包括了jQuery 1.6,还有rails.js for jquery这个。再也不需要之前的那个手工引入jQuery了,或者使用jquery-rails这个gem. 另外还生成有一个/assets/user.js.coffee文件,得要花时间学一下这个coffee-script了。

还发现一个有趣的现象,rails团队似乎偏爱json,在scaffold生成的代码中没有了以前的xml format

1
2
3
4
5
6
7
8
9

def index
  @users = User.all

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @users }
  end
end

另外多了一个assets的generator:

1
2
3
4
5
6
Example:
    `rails generate assets posts`

    Posts assets.
        Javascript:     app/assets/javascripts/posts.js
        Stylesheet:     app/assets/stylesheets/posts.css  

总体上看,变化不大,将jQuery,coffee_script,scss加入默认了。一切都继续演进。