- cd /home/virus
- rails new blog
- cd blog
- rails server
- rails server --environment=production
- rails s -e=production
- rails s -p 8000
- #测试环境,启动console
- rails console
-
- #生产环境,启动console
- rails console production
如果你不想这些操作映射到数据库,你可以执行 rails console—sandbox 通过上面的命令进入console,对数据库做的操作,会在退出之后回滚。
- rails db:create
- rails db:create RAILS_ENV=production
- rails db:migrate
- rails db:migrate RAILS_ENV=production
- rails generate scaffold Product title:string price:decimal description:text
- rails generate controller Persons
- rails generate model Order product_snapshot_id:integer qty:integer product:references
- rails generate migration ProductAddProductNoColumn
rake db:rollback
- #用config/database.yml中production配置创建数据库
- rake db:create RAILS_ENV=production
- #用config/database.yml中production配置迁移数据库
- rake db:migrate RAILS_ENV=production
- #启动rails自带的web服务器,加载config/environments/production.rb 中的配置信息
- rails server --environment=production
- #在开发环境,如果访问的资源还没有编译,就重新编译,默认值是false,如果不设置为true,
- #在生产环境会报错,提示资源没有编译,ActionView::Template::Error (***.css isn’t # precompiled)。
#在开发环境不用设置,因为开发环境总是先编译。
- # config/environments/production.rb
- ...
- config.assets.compile = true
- ...
编译资源文件。
- bundle exec rake assets:precompile
列出所有的route信息
- rake routes
不使用rails自带的单元测试框架
rails new sample_app --skip-test-unit
Gamfile
- source 'https://rubygems.org'
- gem 'rails', '3.2.8'
- group :development, :test do
- gem 'sqlite3', '1.3.5'
- gem 'rspec-rails', '2.11.0'
- end
- # Gems used only for assets and not required
- # in production environments by default.
- group :assets do
- gem 'sass-rails', '3.2.5'
- gem 'coffee-rails', '3.2.2'
- gem 'uglifier', '1.2.3'
- end
- gem 'jquery-rails', '2.0.2'
- group :test do
- gem 'capybara', '1.1.2'
- end
- group :production do
- gem 'pg', '0.12.2'
- end
配置rails使用RSpec替代自带的测试框架
- rails generate rspec:install
- 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
.
- rake db:test:prepare
Model.method
- where
- select
- group
- order
- reorder
- reverse_order
- limit
- offset
- joins
- includes
- lock
- readonly
- from
- having
本文转自 virusswb 51CTO博客,原文链接:http://blog.51cto.com/virusswb/1037125,如需转载请自行联系原作者