每天一剂Rails良药
Rails宝典八十五式:YAML配置文件
我们的程序中可能有一些参数配置,我们可以将这些配置放在外部YAML文件里而不必污染应用程序代码:
# config/initializers/load_config.rb
APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")[RAILS_ENV]
# application.rb
def authenticate ...
by hideto 2007-12-24 浏览 (88) 回复 (1) 关键字: rails yaml configuration
Rails宝典八十四式:Cookie Based Session Store
Rails 2.0将默认使用CookieStore来存储session数据,这符合SNA,很好
如果从Rails 1.x迁移到Rails 2.0,需要这样配置一下:
# in environment.rb
Rails::Initializer.run do |config|
config.action_controller.session = {
:session_key = ...
by hideto 2007-12-20 浏览 (91) 回复 (0) 关键字: cookie session store
Rails宝典八十三式:Migrations in Rails 2.0
Migrations are now sexy in Rails 2.0!
创建当前environme的数据库
rake db:create
创建所有environme的数据库
rake db:create:all
创建和修改Model
script/generate model task name:string priority:integer
script/gene ...
by hideto 2007-12-20 浏览 (75) 回复 (0) 关键字: rails 2.0 migration
Rails宝典八十二式:Rails 2.0之HTTP Basic Authentication
# products_controller.rb
before_filter :authenticate
protected
def authenticate
authenticate_or_request_with_http_basic do |username, password|
username == "foo" && password == "bar"
...
by hideto 2007-12-03 浏览 (287) 回复 (6) 关键字: rails basic
Rails宝典八十一式:Rails2.0之Fixtures尝鲜
Rails 2.0中Fixtures简单多了,看个例子:
# products.yml
couch:
name: Couch
price: 399.99
manufacturer: lazyboy
categories: furniture
tv_stand:
name: TV Stand
price: 149.95
manufacturer: h ...
by hideto 2007-11-27 浏览 (327) 回复 (1) 关键字: rails 2.0 fixtures
Rails宝典八十式:Rails 2.0简化视图
Rails 2.0 里视图应该怎样写?
views/products/index.html.erb:
<%= render :partial => @products %>
views/products/show.html.erb:
<%= render :partial => @product %>
_product.html.erb:
<% div_ ...
by hideto 2007-11-20 浏览 (575) 回复 (2) 关键字: rails 2.0 view
Rails宝典七十九式:生成Named Routes
我们有如下三个近乎一样的named routes:
# routes.rb
ActionController::Routing::Routes.draw do |map|
map.about_company 'about/company', :controller => 'about', :action => 'company'
map.about_company 'about/ ...
by hideto 2007-11-12 浏览 (331) 回复 (2) 关键字: rails named routes
Rails宝典七十八式:生成PDF文档
1,install
gem install pdf-writer
2,require & register
# environment.rb
require 'pdf/writer'
Mime::Type.register 'application/pdf', :pdf
3,use
# lib/product_drawer.rb
def self.draw(produ ...
by hideto 2007-11-05 浏览 (439) 回复 (4) 关键字: pdf
Rails宝典七十七式:禁止JavaScript时的Destroy
Rails里一般我们这样Destroy:
<%= link_to "Destroy", project_path(project), :confirm => "Are you sure?", :method => :delete %>
但是当客户端浏览器禁止JavaScript时就呆了,没有confirm,直接给删除了
所以如果我们需要考虑这种情况的话,需要给出“可降级”的方案,例如 ...
by hideto 2007-10-31 浏览 (360) 回复 (0) 关键字: rails destroy
Rails宝典七十六式:使用scope_out插件
安装
ruby script/plugin install http://scope-out-rails.googlecode.com/svn/trunk/
使用例子
# models/task.rb
scope_out :incomplete, :conditions => ['complete=?', false], :order => 'name'
# tasks_co ...
by hideto 2007-10-22 浏览 (293) 回复 (0) 关键字: scope_out


