Archive for 十二月, 2006

如何在寫一般 Ruby Code 時使用 Rails 的 ActiveRecord 存取 Database

require "rubygems"
require_gem "activerecord"
require "active_record"

ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "hostname",
:database => "yourdb",
:username => "dbuser",
:password => "dbuserpw"
)

class MyTable < ActiveRecord::Base
end

這樣就可以在一般 Ruby Code 中使用 ActiveRecord 的 CRUD (Create, Read, Update, Delete) 存取你的 Database 了.

Update: 2007-03-16

因為 require_gem 已經被廢除了,所以改用 require ‘active_record’ 代替

另外,從 Lighty RoR 最簡潔有力的網頁框架: 在 Rails 之外使用 ActiveRecord 這篇中發現,還可以利用設定 config 的方式將 ActiveRecord 的設定寫在 code 外

想知道詳細用法可至上述網頁看看 :)

張貼留言

How to install Trac on Ubuntu

Prerequisites

  • Apache with SSL
  • SVN

Installation

  1. Install Package
    • $ sudo apt-get install trac
  2. Create the Trac Environments Directory
    • $ sudo mkdir /home/trac
    • $ sudo chown www-data:www-data /home/trac
  3. Setup Apache2
    <VirtualHost servername:443>
        ServerName servername
        ServerAdmin admin@servername
    
        SSLEngine On
        SSLCertificateFile /etc/apache2/ssl/apache.pem
    
        DocumentRoot /var/www/trac
        <Directory />
            Options None
            AllowOverride None
            allow from all
        </Directory>
    
        # Trac settings
        <Directory "/var/www/trac">
            Options +FollowSymLinks
            AllowOverride None
            Order allow,deny
            Allow from all
        </Directory>
    
        <Directory "/var/www/trac/projects">
            AllowOverride None
            Options ExecCGI -MultiViews +FollowSymLinks
            AddHandler cgi-script .cgi
            Order allow,deny
            Allow from all
        </Directory>
    
        # for multiple projects
        RewriteEngine on
        RewriteRule ^/projects/+$            /projects/index.html [L]
        RewriteCond /home/trac/$1            -d
        RewriteRule ^/projects/([[:alnum:]_]+)(/?.*)    /projects/trac.c
    gi$2 [S=1,E=TRAC_ENV:/home/trac/$1]
        RewriteRule ^/projects/(.*)          /projects/index.html
    
        # You need this to allow users to authenticate
        # trac.htpasswd can be created with
        # cmd 'htpasswd -c trac.htpasswd' (UNIX)
        # do 'man htpasswd' to see all the options
        <LocationMatch "/projects/[[:alnum:]_]+/login">
            AuthType Basic
            AuthName "trac"
            require group dev
        </locationMatch>
    
        ErrorLog /var/log/apache2/trac_error.log
    
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
    
        CustomLog /var/log/apache2/trac_access.log combined
        ServerSignature On
    </VirtualHost>
  4. Setup Trac web home directory (/var/www/trac)
    • index.html
    • projects/
      • trac.cgi -> /usr/share/trac/cgi-bin/trac.cgi
    • trac -> /usr/share/trac/htdocs
  5. Restart Apache
    • $ sudo chown -R www-data /usr/share/trac
    • $ sudo apache2 -k restart
  6. Creating Environments
    • $ sudo mkdir /home/trac
    • $ sudo trac-admin /home/trac/YourProjectNameHere initenv
    • $ sudo chown -R www-data /home/trac/YourProjectNameHere
    • The “trac-admin” command shown above prompted me to enter:
      • the project name (YourProjectNameHere)
      • the path to svn repository (/home/svn/YourProjectNameHere)
      • the path to the Trac templates directory (/usr/share/trac/templates)
  7. Access your project
    • https://servername/project/YourProjectNameHere

Reference Link:

TracOnUbuntu – The Trac Project – Trac

留言 (6)

BACKUP and RESTORE WIKI/SVN/TRAC

WIKIDB

  • Backup
    • mysqldump -u root -pxxxxxx wikidb > backup.sql
  • Restore
    • mysql -u root -pxxxxxx wikidb < backup.sql

SVN

  • Backup
    • svnadmin dump /svn/repository_path > dumpfile
  • Restore
    • svnadmin load /svn/repository_path < dumpfile

TRAC

  • Backup
    • Copy /trac/projrct
  • Restore
    • Just put the copy back…

張貼留言

Simple ruby send mail example.

require ‘net/smtp’
msg = <<END_OF_MESSAGE

From: from_alias <from_where>
To: to_alias <to_where>
Subject: Ruby Mail Test

As title. This is a test mail sending by ruby.

END_OF_MESSAGE

Net::SMTP.start() do |smtp|
smtp.sendmail( msg, “from_where”, “to_where” )
end

That’s all.:D

Reference Link:
sendmail (Net::SMTP)

留言數(1)

【Book】次世代─Linux Ubuntu 玩全手冊

次世代Linux-Ubuntu玩全手冊【博客來介紹 】

這是長官K要我抱回來的新書(不曉得能不能報公帳:D)也應該是 Ubuntu 的第一本正體中文詳細介紹手冊,之前雖然施威銘研究室也出了一本「Linux 新天王 Ubuntu 6.10」,不過只是簡單的介紹沒什麼實質意義,有興趣的人可以去買來看看。

BTW, Ubuntu 正體中文 Wiki 裡頭的東西也相當豐富喔~

留言數(1)

受保護:Some Ruby on Rails E-books

本文已被密碼保護.請鍵入密碼瀏覽:


輸入密碼才能查看留言