2008年1月的存档

Ruby on Rails 2.0.2 中scaffold的使用

2008年01月03日 星期四

照着《Web开发敏捷之道》,写里面的depot例子,一开始就遭遇挫折:

class AdminControll < ApplicationController
 
scaffold :product
end

加了scaffold :product之后,运行提示:
undefined method ’scaffold’ for ActionController:Class.

直接用后面的
depot>runy script/generate scaffold product admin
也不管用

上网查了查,应该是2.0.2中不再这样支持了,要用下面的语法:
depot>ruby script/generate scaffold product title:string description:text image_url:string

注意,网上有的地方是写
ruby script/generate scaffold ModelName [field:type, field:type]
但实际上不能写[], 也不能写, ,在这上面折腾了比较长时间. 最后还是通过直接运行ruby script/generate scaffold看它的帮助和示例才明白过来。

这样生成的001_create_products.rb里面也自动定义了相关的字段,不用手工添加了。自动生成的和书上例子里面也稍有不同,书中例子是:

class CreateProducts < ActiveRecord::Migration
 
def self.up
    
create_table :products do |t|
      
t.column :title, :string
      
t.column :description, :text
      
t.column :image_url, :string
 
      
t.timestamps
    
end
 
end
 
 
def self.down
    
drop_table :products
 
end
end

自动生成的是:

class CreateProducts < ActiveRecord::Migration
 
def self.up
    
create_table :products do |t|
      
t.string :title
      
t.text :description
      
t.string :image_url
 
      
t.timestamps
    
end
 
end
 
 
def self.down
    
drop_table :products
 
end
end

第一个ruby程序

2008年01月03日 星期四

现在手头的项目隔三岔五地要往用户的服务器上传当前版本,总是要打开FTP客户端来传。应该自动化这个工作的,正好学习ruby中,就写了个ruby脚本,可以按照当前时间自动传。

require 'net/ftp'
 
date = $*[0];
if date == nil
 
date = Time.now.strftime('%y%m%d')
end
 
ftp = Net::FTP.new('ftp.examples.com')
ftp.login('username', 'password')
ftp.chdir('download')
 
filename = 'history.htm'
puts "sending #{filename}"
ftp.putbinaryfile(filename, filename, 1024)
 
filename = 'shocs' + date + '.rar'
puts "sending #{filename}"
ftp.putbinaryfile(filename, filename, 1024)
 
ftp.close