Rails Will_paginate 对数组分页

首先引入will_paginate/array文件

config/application.rb

1
require 'will_paginate/array'
1
2
3
4
def index
  #如果数据不是 数组格式,需要转换为数组格式,别的就正常一样
  @servers = f.servers.all.to_a.paginate :page => params[:page], :order => 'created_at desc', :per_page => 5
end

Scss 安装使用

2.1 安装 SASS是Ruby语言写的,但是两者的语法没有关系。不懂Ruby,照样使用。只是必须先安装Ruby,然后再安装SASS。 假定你已经安装好了Ruby,接着在命令行输入下面的命令:   gem install sass 然后,就可以使用了。 2.2 使用 SASS文件就是普通的文本文件,里面可以直接使用CSS语法。文件后缀名是.scss,意思为Sassy CSS。 下面的命令,可以在屏幕上显示.scss文件转化的css代码。(假设文件名为test。)   sass test.scss 如果要将显示结果保存成文件,后面再跟一个.css文件名。   sass test.scss test.css SASS提供四个编译风格的选项:    nested:嵌套缩进的css代码,它是默认值。    expanded:没有缩进的、扩展的css代码。    compact:简洁格式的css代码。    compressed:压缩后的css代码。 生产环境当中,一般使用最后一个选项。   sass —style compressed test.sass test.css 你也可以让SASS监听某个文件或目录,一旦源文件有变动,就自动生成编译后的版本。   // watch a file   sass —watch input.scss:output.css   // watch a directory   sass —watch app/sass:public/stylesheets SASS的官方网站,提供了一个在线转换器。你可以在那里,试运行下面的各种例子。 三、基本用法 3.1 变量 SASS允许使用变量,所有变量以$开头。   $blue : #1875e7;    div {    color : $blue;   } 如果变量需要镶嵌在字符串之中,就必须需要写在#{}之中。   $side : left;   .rounded {     border-#{$side}-radius: 5px;   } 3.2 计算功能 SASS允许在代码中使用算式:   body {     margin: (14px/2);     top: 50px + 100px;     right: $var * 10%;   } 3.3 嵌套 SASS允许选择器嵌套。比如,下面的CSS代码:   div h1 {     color : red;   } 可以写成:   div {     hi {       color:red;     }   } 属性也可以嵌套,比如border-color属性,可以写成:   p {     border: {       color: red;     }   } 注意,border后面必须加上冒号。 在嵌套的代码块内,可以使用$引用父元素。比如a:hover伪类,可以写成:   a {     &:hover { color: #ffb3ff; }   } 3.4 注释 SASS共有两种注释风格。 标准的CSS注释 / comment / ,会保留到编译后的文件。 单行注释 // comment,只保留在SASS源文件中,编译后被省略。 在/后面加一个感叹号,表示这是”重要注释”。即使是压缩模式编译,也会保留这行注释,通常可以用于声明版权信息。   /!     重要注释!   */ 四、代码的重用 4.1 继承 SASS允许一个选择器,继承另一个选择器。比如,现有class1:   .class1 {     border: 1px solid #ddd;   } class2要继承class1,就要使用@extend命令:   .class2 {     @extend .class1;     font-size:120%;   } 4.2 Mixin Mixin有点像C语言的宏(macro),是可以重用的代码块。 使用@mixin命令,定义一个代码块。   @mixin left {     float: left;     margin-left: 10px;   } 使用@include命令,调用这个mixin。   div {     @include left;   } mixin的强大之处,在于可以指定参数和缺省值。   @mixin left($value: 10px) {     float: left;     margin-right: $value;   } 使用的时候,根据需要加入参数:   div {     @include left(20px);   } 下面是一个mixin的实例,用来生成浏览器前缀。   @mixin rounded($vert, $horz, $radius: 10px) {     border-#{$vert}–#{$horz}-radius: $radius;     -moz-border-radius-#{$vert}#{$horz}: $radius;     -webkit-border-#{$vert}–#{$horz}-radius: $radius;   } 使用的时候,可以像下面这样调用:   #navbar li { @include rounded(top, left); }   #footer { @include rounded(top, left, 5px); } 4.3 颜色函数 SASS提供了一些内置的颜色函数,以便生成系列颜色。   lighten(#cc3, 10%) // #d6d65c   darken(#cc3, 10%) // #a3a329   grayscale(#cc3) // #808080   complement(#cc3) // #33c 4.4 插入文件 @import命令,用来插入外部文件。   @import “path/filename.scss”; 如果插入的是.css文件,则等同于css的import命令。   @import “foo.css”; 高级用法

条件语句

@if可以用来判断:

1
2
3
4
  p {
    @if 1 + 1 == 2 { border: 1px solid; }
    @if 5 < 3 { border: 2px dotted; }
  }

配套的还有@else命令:

1
2
3
4
5
  @if lightness($color) > 30% {
    background-color: #000;
  } @else {
    background-color: #fff;
  }

循环语句

SASS支持for循环:

1
2
3
4
5
  @for $i from 1 to 10 {
    .border-#{$i} {
      border: #{$i}px solid blue;
    }
  }

也支持while循环:

1
2
3
4
5
  $i: 6;
  @while $i > 0 {
    .item-#{$i} { width: 2em * $i; }
    $i: $i - 2;
  }

each命令,作用与for类似:

1
2
3
4
5
  @each $member in a, b, c, d {
    .#{$member} {
      background-image: url("/image/#{$member}.jpg");
    }
  }

自定义函数

SASS允许用户编写自己的函数。

1
2
3
4
5
6
  @function double($n) {
    @return $n * 2;
  }
  #sidebar {
    width: double(5px);
  }

JsTree Jquery Plugin

Github Address: [https://github.com/vakata/jstree]

preview

Make Any Ruby Object Feel Like ActiveRecord

Before I Begin, The ActiveModel API Before I begin, there are two major elements to ActiveModel. The first is the ActiveModel API, the interface that models must adhere to in order to gain compatibility with ActionPack’s helpers. I’ll be talking more about that soon, but for now, the important thing about the ActiveModel API is that your models can become ActiveModel compliant without using a single line of Rails code.

In order to help you ensure that your models are compliant, ActiveModel comes with a module called ActiveModel::Lint that you can include into your test cases to test compliance with the API:

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
class LintTest < ActiveModel::TestCase
  include ActiveModel::Lint::Tests

  class CompliantModel
    extend ActiveModel::Naming

    def to_model
      self
    end

    def valid?()      true end
    def new_record?() true end
    def destroyed?()  true end

    def errors
      obj = Object.new
      def obj.[](key)         [] end
      def obj.full_messages() [] end
      obj
    end
  end

  def setup
    @model = CompliantModel.new
  end
end

The ActiveModel::Lint::Tests provide a series of tests that are run against the @model, testing for compliance.

ActiveModel Modules

The second interesting part of ActiveModel is a series of modules provided by ActiveModel that you can use to implement common model functionality on your own Ruby objects. These modules were extracted from ActiveRecord, and are now included in ActiveRecord.

Because we’re dogfooding these modules, you can be assured that APIs you bring in to your models will remain consistent with ActiveRecord, and that they’ll continue to be maintained in future releases of Rails.

The ActiveModel comes with internationalization baked in, providing an avenue for much better community sharing around translating error messages and the like.

The Validations System

This was perhaps the most frustrating coupling in ActiveRecord, because it meant that people writing libraries for, say, CouchDB had to choose between painstakingly copying the API over, allowing inconsistencies to creep in, or just inventing a whole new API.

Validations have a few different elements.

First, declaring the validations themselves. You’ve seen the usage before in ActiveRecord:

1
2
3
class Person < ActiveRecord::Base
  validates_presence_of :first_name, :last_name
end

To do the same thing for a plain old Ruby object, simply do the following:

1
2
3
4
5
6
7
8
9
10
class Person
  include ActiveModel::Validations

  validates_presence_of :first_name, :last_name

  attr_accessor :first_name, :last_name
  def initialize(first_name, last_name)
    @first_name, @last_name = first_name, last_name
  end
end

Rails程序自启动

文件存储位置为/etc/init.d/

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
#! /bin/sh

#http://blog.kiskolabs.com/post/722322392/unicorn-init-scripts

### BEGIN INIT INFO
# Provides:          unicorn
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the unicorn web server
# Description:       starts unicorn
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/unicorn_rails
DAEMON_OPTS="-c /home/wwwroot/matz.cn/config/unicorn.rb -E production -D"
NAME=unicorn_rails
DESC=unicorn_rails
PID=/home/wwwroot/matz.cn/tmp/pids/unicorn.pid

case "$1" in
  start)
    echo -n "Starting $DESC: "
    $DAEMON $DAEMON_OPTS
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
        kill -QUIT `cat $PID`
    echo "$NAME."
    ;;
  restart)
    echo -n "Restarting $DESC: "
        kill -QUIT `cat $PID`
    sleep 1
    $DAEMON $DAEMON_OPTS
    echo "$NAME."
    ;;
  reload)
        echo -n "Reloading $DESC configuration: "
        kill -HUP `cat $PID`
        echo "$NAME."
        ;;
  *)
    echo "Usage: $NAME {start|stop|restart|reload}" >&2
    exit 1
    ;;
esac

exit 0

通过chkconfig命令添加自启动:

1
2
chkconfig --add <name>
chkconfig <name> on

至此,重启系统后,该Rails程序用随系统启动。

Ruby 连接 Sql Server

因为工作需要,要分析存放在SQL Server上的数据,所以不得不研究一下如何使用Ruby访问SQL Server,发现其实还是很简单的:

== 安装FreeTDS ==


  • 安装预要求的包:

ruby aptitude install freetds-dev

  • 3.安装Tiny_TDS Tiny_TDS,安装和使用非常简单,推荐使用:

ruby sudo gem install tiny_tds

用[https://github.com/rails-sqlserver/tiny_tds]访问SQL Server很简单:

1
2
3
4
5
6
7
  require 'tiny_tds'
  client = TinyTds::Client.new(:username => 'fankai', :password => 'fankai', :host => '192.168.0.1', :database => 'test')
  result = client.execute("select top 10 * from User");
  result.each do |row|
    puts row
  end

1
gem install activerecord-sqlserver-adapter

配置database.yml如下:

1
2
3
4
5
6
  development:
    adapter: sqlserver
    host: mydb.net
    database: myapp_development
    username: sa
    password: secret

原文见[http://robbinfan.com/blog/44/ruby-connect-sqlserver]

安装R

debian系统下,不建议使用编译安装方式进行安装,以避免无法生成png格式图片的错误,以下为debian系统使用aptitude安装R的过程

  • 添加R3以上版本的网络源
1
2
$ nano -w /etc/apt/sources.list
deb http://mirrors.ustc.edu.cn/CRAN/bin/linux/debian squeeze-cran3/
  • 添加key 方式1:执行以下命令
1
$ apt-key adv --keyserver subkeys.pgp.net --recv-key 381BA480

方式2: 创建名为key的文本,文本内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: SKS 1.1.0

mQGiBEXUFiURBADkTqPqcRYdLIguhC6fnwTvIxdkoN1UEBuPR6NYW4iJzvRSas/g5bPo5ZxE
2i5BXiuVfYrSk/YiU+/lc0K6VYNDygbOfpBgGGhtfzYfFRTYNq8QsdD8L8BMYtOu5rYo5BYt
a0vuantIS9mn9QnH7885uy5tX/TXO7ICYVHxnFNr2wCguNtMdz9+DRQ38n4iiHzTtj/7UHsD
/0+0TLHHvY1FfakVinamR9oCm9uH9PmkGTy6jRnrvg5Z+TTgygiDdTBKPc1TqpFgoFtqh8G5
DpDPbyh5GzBj8Ky1mBJb3bMwy2RUth1cztHEWI36xuCl+KrLtA4OYuCwJJZhOWDIO9aO2LW5
kJhIwIuvSrEtOgTxpzy82g7eEzvLBADUrQ01fj+9VDrO2Vept8jtaGK+4kW3cBAG/UbOrTjt
63VurXwyvNb6q7hKFUaVH42Fc0e64F217mutCyftPWYJwY4SR8hUmjEM/SYcezUDWWvVxmkF
8M4rMhHa0j+q+et3mTKwgxehQO9hLUqRebnmJuwNqNJKb9izsPqmh83Zo7Q7Sm9oYW5uZXMg
UmFua2UgKENSQU4gRGViaWFuIGFyY2hpdmUpIDxqcmFua2VAdW5pLWJyZW1lbi5kZT6IRgQQ
EQIABgUCRdQ+nQAKCRAvD04U9kmvkJ3+AJ4xLMELB/fT1AwtR1azcH0lKg/TegCdEvtp3SUf
aHP3Jvg2CkzTZOatfFuIRgQQEQIABgUCS4VoCgAKCRDvfNpxC67+5ZFyAKCAzgPTqM6sSMhB
iaZbNCpiVtwDrQCgjMy+iqPm7SVOCq0XJsCCbxymfB+IXgQTEQIAHgUCRdQWJQIbAwYLCQgH
AwIDFQIDAxYCAQIeAQIXgAAKCRAG+Q3lOBukgM09AKCuapN6slttAFRjs2/mgtaMMwO9sgCf
ZD2au39Oo8QLXZhZTipN8c7j9mM=
=BRgm
-----END PGP PUBLIC KEY BLOCK-----

添加key

1
$ apt-key add key

更新源列表

1
$ aptitude update
  • 安装R
1
$ aptitude install r-base r-base-dev

Install Erlang

Erlang依赖哪些库

  • A fully working GCC compiler environment
  • Ncurses development libraries
  • OpenSSL development libraries

安装了这些库之后,必须要重新执行configure命令,configure之后会有提示哪些依赖的库没有安装,可以根据你的需要放弃安装一些库;上面的操作可以使用下面的命令实现:

1
2
3
4
5
  wget http://www.erlang.org/download/otp_src_R13B04.tar.gz
  tar xfvz otp_src_R13B04.tar.gz
  cd otp_src_R13B04/
  ./configure --with-ssl
  sudo make install