1. cd /home/virus 
  2.  
  3.  
  4. rails new blog 
  5.  
  6.  
  7. cd blog 
  8.  
  9.  
  10. rails server 
  11. rails server --environment=production
  12. rails s -e=production
  13. rails s -p 8000
  14.   
  15.  
  16. #测试环境,启动console 
  17.  
  18. rails console 
  19.  
     
        
    • #生产环境,启动console 
    • rails console production
  执行rails console之后,使用development环境启动,所做的修改会映射到数据库
如果你不想这些操作映射到数据库,你可以执行   rails console—sandbox   通过上面的命令进入console,对数据库做的操作,会在退出之后回滚。

 

  1.  
  2.  
  3.  
  4. rails db:create 
  5.  
  6. rails db:create RAILS_ENV=production
  7.  
  8.  
  9. rails db:migrate 
  10. rails db:migrate RAILS_ENV=production
  1.  
  2. rails generate scaffold Product title:string price:decimal description:text 
  3.  
  4.  
  5. rails generate controller Persons 
  6.  
  7.  
  8. rails generate model Order product_snapshot_id:integer qty:integer product:references 
  9.  
  10.  
  11. rails generate migration ProductAddProductNoColumn 
  12.  
  13.   
  14.  
  15.   

 

rake db:rollback

 

 

 
  
  1. #用config/database.yml中production配置创建数据库
  2. rake db:create RAILS_ENV=production 
  3.  
  4.   
  5. #用config/database.yml中production配置迁移数据库
  6. rake db:migrate RAILS_ENV=production 
  7.  
  8.   
  9. #启动rails自带的web服务器,加载config/environments/production.rb 中的配置信息
  10. rails server --environment=production 

 

 

 
  
  1.  
  2. #在开发环境,如果访问的资源还没有编译,就重新编译,默认值是false,如果不设置为true,
  3. #在生产环境会报错,提示资源没有编译,ActionView::Template::Error (***.css isn’t # precompiled)。

#在开发环境不用设置,因为开发环境总是先编译。

  1.  
  2. # config/environments/production.rb 
  3. ... 
  4. config.assets.compile = true 
  5. ... 

 

 编译资源文件。

 
  
  1. bundle exec rake assets:precompile 

 

列出所有的route信息

 
  
  1. rake routes 

 

不使用rails自带的单元测试框架

rails new sample_app --skip-test-unit

Gamfile

 
  
  1. source 'https://rubygems.org' 
  2.  
  3. gem 'rails''3.2.8' 
  4.  
  5. group :development:test do 
  6.   gem 'sqlite3''1.3.5' 
  7.   gem 'rspec-rails''2.11.0' 
  8. end 
  9.  
  10. # Gems used only for assets and not required 
  11. # in production environments by default. 
  12. group :assets do 
  13.   gem 'sass-rails',   '3.2.5' 
  14.   gem 'coffee-rails''3.2.2' 
  15.   gem 'uglifier''1.2.3' 
  16. end 
  17.  
  18. gem 'jquery-rails''2.0.2' 
  19.  
  20. group :test do 
  21.   gem 'capybara''1.1.2' 
  22. end 
  23.  
  24. group :production do 
  25.   gem 'pg''0.12.2' 
  26. end 

配置rails使用RSpec替代自带的测试框架

 
  
  1. rails generate rspec:install 
 
  
  1. rspec spec/request/*.rb 
rails generate integration_test user_pages

 

为测试环境生成数据库

This just ensures that the data model from the development database, db/development.sqlite3, is reflected in the test database, db/test.sqlite3.

 
  
  1. rake db:test:prepare 

 

 

Model.method

 
  
  1. where 
  2. select 
  3. group 
  4. order 
  5. reorder 
  6. reverse_order 
  7. limit 
  8. offset 
  9. joins 
  10. includes 
  11. lock 
  12. readonly 
  13. from 
  14. having