创建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