創建Shop項目
$ rails new shop
利用scaffold創建資源
$ rails generate scaffold production name:text price:float amount:integer
顯示rails爲資源分配的routes地址
$ rake routes | grep production
DB Migrate
$ bin/rails db:migrate RAILS_ENV=development
Start Server
development環境下,修改code無需重啓server。
$ rails server
設置首頁爲productions/index
在Routes.rb中進行設置,有2種設置方法
get '/' => 'productions#index'
root 'productions#index'
給資源添加訪問地址
get '/addProduction' => 'productions#new'
match 規則 & 傳參
如果需要傳參
match '/addProduction/:name/:price' => 'productions#new'
那麼之後就可以在controller中獲取這兩個參數
@name = params[:name]
rails 常用命令
Usage: rails COMMAND [ARGS] The most common rails commands are: generate Generate new code (short-cut alias: "g") console Start the Rails console (short-cut alias: "c") server Start the Rails server (short-cut alias: "s") test Run tests (short-cut alias: "t") dbconsole Start a console for the database specified in config/database.yml (short-cut alias: "db") new Create a new Rails application. "rails new my_app" creates a new application called MyApp in "./my_app" All commands can be run with -h (or --help) for more information.
generate 常用命令
esxi23v113@esxi23v113:~/Aptana Studio Workspace/blog$ rails generate -h Running via Spring preloader in process 11877 Expected string default value for '--jbuilder'; got true (boolean) Usage: rails generate GENERATOR [args] [options] General options: -h, [--help] # Print generator's options and usage -p, [--pretend] # Run but do not make any changes -f, [--force] # Overwrite files that already exist -s, [--skip] # Skip files that already exist -q, [--quiet] # Suppress status output Please choose a generator below. Expected string default value for '--helper'; got true (boolean) Expected string default value for '--assets'; got true (boolean) Rails: assets channel controller generator helper integration_test jbuilder job mailer migration model resource scaffold scaffold_controller task Coffee: coffee:assets Js: js:assets TestUnit: test_unit:generator test_unit:plugin