基本方式
match
'products/:id'
=>
'products#show'
,via
::get
簡寫
get
'products/:id'
=>
'products#show'
post
'products'
=>
'products#create'
同乙個url對應多個http method
match
'products/:id'
=>
'products#show'
,via:[
:get
,:post
]
乙個url對應所有http method
match
'products'
=>
'products#index'
,via
::any
基本方式
get
'products/:id'
=>
'products#show'
這樣的話 = 4;
link_to
"products"
,controller
:"products"
,action
:"show",id
:1
如下所示,用小括號()
aatch
':controller(/:action(/:id(.:format)))'
,via
::any
定義routes時直接定義跳轉邏輯,使用redirect方法
get
"/google",to
:redirect
('')
redirect方法接受block,可以接收params引數
下面實現了api的version跳轉,api內部邏輯公升級,但對外介面保持不變
match
'api/v1/:api',to
:redirect"},
via:
any
redirect方法可以設定跳轉時的status
match
"/api/v1/:api",to
:redirect
(status
:302)"
},via
::any
get
':controller/show/:id'
=>
:show
,constraints
:get
':controller/show/:id'
=>
:show_error
上面的例子裡就不需要在controller裡判斷id是否合法了。另外routes裡預設正則預設加\a#regexp#\z的
由於經常要對id的格式做限制,上面的例子有簡寫的辦法
get
':controller/show/:id'
=>
:show,id
:/\d+/
get':controller/show/:id'
=>
:show_error
constraints也可以接收block
get
'records/:id'
=>
"records#protected"
,constraints
:proc
對於這個url /items/list/base/books/fiction
get
'items/list/*specs'
,controller
:'items'
,action
:'list'
可以得到params[:specs] = base/books/fiction
基本用法
get
'help'
=>
'help#index'as:
'help'
link_to
"help"
,help_path
level up
對於下面的link_to呼叫
get
"item/:id"
=>
"items#show"
link_to
"auction of #"
,controller
:"items"
,action
:"show",id
:item
.id
可以定義為
get
"item/:id"
=>
"items#show",as
:"item"
link_to
"auction of #"
,item_path(id
:item.id
)
加點語法糖衣,等於
link_to
"auction of #"
,item_path
(item.id
)link_to
"auction of #"
,item_path
(item
)
level up
對於多個引數,要按順序傳
get
"auction/:auction_id/item/:id"
=>
"items#show",as
:"item"
link_to
"auction of #"
,item_path
(auction
,item
)
level up
隱藏id,建立更可讀的url
defto_param
description
.parameterize
enditem_path
(auction
,item
)=>
/auction/3/
item
/cello
-bow
scope之前
get
'auctions/new'
=>
'auctions#new'
get'auctions/edit/:id'
=>
'auctions#edit'
post
'auctions/pause/:id'
=>
'auctions#pause'
scope之後
scope
controller
::auctions
doget
'auctions/new'
=>
:new
get'auctions/edit/:id'
=>
:edit
post
'auctions/pause/:id'
=>
:pause
end
scope path後
scope
path
:'/auctions'
,controller
::auctions
doget
'new'
=>
:new
get'edit/:id'
=>
:edit
post
'pause/:id'
=>
:pause
end
使用name prefix
scope
:auctions,as
:'admin'
doget
'new'
=>
:new,as
:'new_auction'
#=>link_to '***', admin_new_auction_url
end
namespace隱式定義了controller和path以及prefix name
namespace
:auctions
,:controller
=>
:auctions
doget
'new'
=>
:new
get'edit/:id'
=>
:edit
post
'pause/:id'
=>
:pause
end
Rails4 使用postgreSQL做資料庫
1.首先在ubuntu下安裝postgresql sudo apt get install postgresql2.啟動postgresql伺服器 sudo etc init.d postgresql start3.登陸postgresql postgresql安裝完成後預設只有乙個使用者 就是po...
陣列基操三連(4)
給定乙個長度為n的整型陣列arr,其中有n個互不相等的自然數1 n 請實現arr的排序 但是不要把下標0 n 1位置上的數值通過直接賦值的方式替換成1 n。要求 時間複雜度為o n 額外空間複雜度為o 1 思路 從左向右檢查,檢查到需要換的以後,就直接把它放到該去的位置,然後被換掉的數,位置肯定也不...
html5學習筆記4 XHTML HTML基礎
2.2 xhtml基礎 xhtml文件與html文件沒有太大區別,只是新增了xml語言的基本規範和要求。2.2.1 xhtml結構 與html不同之處 1 定義文件型別 dtd表示文件型別定義,瀏覽器根據預定義的dtd來解析頁面元素。2 宣告命名空間 向xml過渡 要符合xml規則 需要命名空間 x...