Rails 簡單的小例子 —— 新建一個購物網站

創建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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章