锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
3.Our controller interacts with the models in our application to gather the necessary data
4.Our model may retrive or insert data to our database
5.Once our models have generated or retrived correct information,they return the data back to the collection
6.The controller collects all of the data it's received from models and selects a view template to render
7.The view template is rendered using the data that the collection gathered and handed to the web server
8.An html page is returned to the user.
]]>
/app/controllers/stories_controllers.rb:
class StoriesController<ActionControllers::Base
def index
@varies="hello world"
end
end
/views/stories/index.html.erb:
<%=@varies%>
/config/routes.rb
ActionController::Routing::Routes.draw do |map|
map 'stories',:controller=>'stories',:action=>'index'
end
鍚姩鏈嶅姟鍦↖E涓墦寮:
http://localhost:3000/stories
OK
]]>
http://guides.rails.info/routing_outside_in.html
榪欑瘒鏂囩珷灝嗕粙緇峈ails routing閽堝鐢ㄦ埛鏂歸潰鐨勪竴浜涚壒鎬?鍙傝冭繖綃囨枃绔?浣犲皢浼氬鍒頒互涓嬬煡璇?
a.鐞嗚Вrouting鐨勪綔鐢?br />
b.鐮磋Вroutes.rb鍐呯殑浠g爜
c.鏋勫緩浣犺嚜宸辯殑routes,鍙互鐢╟lassic hash鏍峰紡鎴栫幇鍦ㄦ祦琛岀殑RESTful鏍峰紡.
d.璇嗗埆route鎬庢牱涓巆ontroller鍜宎ction鏄犲皠.
1.The Dual Purpose of Routing
Rails routing 鏈変袱縐嶆満鍒?浣犲彲浠ュ皢trees杞崲涓簆ager,鎴栨妸paper杞崲鍥瀟rees.鍏蜂綋鍦拌,瀹冨彲浠ヨ繛鎺ユ敹鍒扮殑璇鋒眰涓庝綘搴旂敤紼嬪簭鐨勬帶鍒跺櫒鐨勪唬鐮佸拰甯綘鐢熸垚URLs,鑰屼笉鐢ㄥ仛涓轟竴涓瓧絎︿覆紜紪鐮?
1.1connecting URLs to Code;
褰撲綘鐨勫簲鐢ㄧ▼搴忔敹鍒扮殑璇鋒眰涓?
GET /patients/17
Rails閲岀殑璺敱寮曟搸灝辨槸涓孌靛垎鍙戣繖涓姹傚埌搴旂敤紼嬪簭鍚堥傜殑浣嶇疆榪涜澶勭悊鐨勪竴孌典唬鐮?鍦ㄨ繖涓渚嬩腑,榪欎釜搴旂敤紼嬪簭寰堝彲鑳戒互榪愯patients鎺у埗鍣ㄩ噷鐨剆how緇撴潫.鏄劇ずpatients ID涓?7鐨勮緇嗕俊鎭?
1.2 Generateing URLs from Code
Routing 涔熷彲浠ュ弽榪囨潵榪愯,濡傛灉浣犵殑搴旂敤紼嬪簭涓寘鍚繖鏍風殑浠g爜:
@patient=Patient.find(17)
<%= link_to "Patient Record",patient_path(@patient)%>
榪欐椂璺敱寮曟搸杞崲榪欎釜閾炬帴鍒頒竴涓猆RL:http://example.com/patients/17.浠ヨ繖縐嶆柟寮忎綘鍙互闄嶄綆搴旂敤紼嬪簭鐨勮剢寮辨?浣夸綘鐨勪唬鐮佹洿鍔犲鏄撻槄璇誨拰鐞嗚В.
Patient 蹇呴』浣滀負涓涓猺esource琚0鏄庝負涓涓祫婧?閫氳繃named route鏉ヨ漿鎹?
2.Quick Tour of Routes.rb
鍦≧ails涓璻outing鏈変袱縐嶇粍浠?routing engine鏈韓,瀹冨仛涓篟ails鐨勪竴閮ㄥ垎,config/routes.rb鏂囦歡,瀹冨寘鍚疄闄呯殑鍙敤鍦ㄥ簲鐢ㄧ▼搴忎腑鐨剅outes.
2.1 Processing the File
鍦ㄥ艦寮忎笂,Routes.rb鏂囦歡涔熷氨鏄竴涓ぇ澶х殑block,浼氳鏀懼叆ActionController::Routing::Routes.draw.
鍦ㄨ繖涓枃浠朵腑鏈変簲縐嶄富瑕佺殑鏍峰紡:
RESTful Routes
Named Routes
Nested Routes
Regular Routes
Default Routes
2.2 RESTful Routes
RESTful Routes 鍒╃敤rails宓屽叆寮廟EST鏂規硶鏉ュ皢routing鐨勬墍鏈変俊鎭寘瑁呬負涓涓崟鐙殑澹版槑.eg: map.resource :books
2.3 named Routes
named routes 鍦ㄤ綘鐨勪唬鐮佷腑緇欎綘寰堝彲璇葷殑閾炬帴,涔熷彲浠ュ鐞嗘敹鍒扮殑璇鋒眰
map.login '/login' ,:controller=>'session',:action=>'new'
2.4 Nested routes
Nested routes鍙互鍦ㄤ竴涓祫婧愰噷澹版槑鍙︿竴涓祫婧?
map.resources :assemblies do |assemblies|
assemblies.resources :parts
end
2.5 Regular Routes
map.connect 'parts/:number',:controller=>'inventory',:action=>'show'
2.6 Default Routes
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
routes鐨勫懡鍚嶅厑璁鎬綘鍑忓皯閲嶅鐨勪唬鐮?閫氳繃鍒嗛厤涓涓牴鎹敓鎴愪竴涓皝瑁卹outes rule hash鐨勬柟娉曠殑璺緞瑙勫垯鍚嶇О鏉ュ疄鐜?
浣犲彲浠ュ畾涔変竴涓懡鍚嶈礬寰?浣犲彲浠ヨ皟鐢ㄥ畠鍦ㄤ綘鐨剅outes.rb鏂囦歡涓潵浠f浛connect鏂規硶.
渚嬪:
map.home '',:controller=>'main',:action=>'start'
榪欏彧鏄負浣犲仛浜嗗緢灝戠殑涓鐐逛簨.棣栧厛,瀹冨垱寤轟竴涓悕涓篽ome_url鐨勬柟娉?鍥犳涓婇潰鐨勫懡鍚嶈鍒?鍓嶆湡鐨勮姹備負:
redirect_to :controller=>'main',:action=>'start' 鐜板湪涓?
redirect_to home_url
鍥犱負*_to鏂規硶琚噸鍐欑殑鏂瑰紡,鍦ㄤ笂闈㈢殑鎯呭喌,浣犳病鏈夊皢鍙傛暟浼犻掔粰鍛藉悕璺緞,浣犱篃鍙互灝嗗畠浼犻掔粰涓涓紩鐢ㄧ殑鏂規硶.
redirect_to :home_url
娉ㄦ剰:灝嗗懡鍚嶈礬寰勫仛涓轟竴涓鍙峰凡緇忓彇娑堜簡,鍦╮ails2.0灝嗚鍒犻櫎.
榪欎釜涔熻兘搴旂敤浜庢槸tests.濡傛灉鍦ㄤ綘鐨刢ontroller閲屾湁redirect_to home_url,涔熷彲浠ョ敤 assert_redirect_to home_url.
*_url鏂規硶闇瑕佷竴涓敮涓鐨勫彲閫夊弬鏁?榪欎釜鍙傛暟鏄竴涓猦ash,鎻掑叆url_for涔嬩腑,榪欐牱鍋氬氨鏄負浜嗗綋鐢╮edirect_to,link_to鏃跺懡鍚嶈鍒欏彲浠ュ弬鏁板寲.
浣犲鏋滆鏈夎繖鏍風殑route
map.user_page 'users/:user',:controller=>'users',:action=>'show'
浣犲彲浠ヨ繖鏍峰仛
link_to @user.username,user_page_url(:user=>@user)
娉ㄩ噴:榪欎釜浠呬粎璞℃櫘閫氱殑routes,琛ㄥ崟鏄痯ath_part/:symbol.鍦ㄤ笂闈㈢殑渚嬪瓙涓?user榪欓儴鍒嗗皢鍙睍紺哄湪璺緞涓?騫朵笉涓ユ牸涓巆ontroller鐩稿叧鑱?users/鍙槸path鐢熸垚鐨勯儴鍒?
涓婇潰鐨勪緥瀛愮敓鎴?lt;a href="http://localhost/users/7">http://localhost/users/7</a>
姣斿:
:user 鏄綘瀹氫箟鐨勫弬鏁板悕縐?濡傛灉瀹氫箟涓や釜涓妯′竴鏍風殑璺緞,寰堝彲鑳藉緱涓嶅埌浣犳兂瑕佺殑緇撴灉.瀵逛簬routes,瀹氫箟榛樿鍊?
褰撶敤鍙傛暟鏉ュ懡鍚峳outes,浣犲繀欏諱紶閫掑弬鏁扮粰assert涓殑鍛藉悕瑙勫垯.濡傛灉鍦ㄤ綘鐨刢ontroller涓湁redirect_to user_page_url(:user=>@user), 浣犻渶瑕佹湁assert_redirected_to user_page_url(:user=>@user).controller涓瑻user鐨刬d蹇呴』涓巘est涓殑@user鐨刬d鍖歸厤.
闄や簡[route_name]_url鏂規硶,浣犲彲浠ョ敤hash_for_[route_name]_url鏂規硶,榪欎釜鏂規硶鍖呰hash,娌℃湁璋冪敤url_for
鎵鏈夌殑reoute灝嗗叾瀹冪殑鍛藉悕瑙勫垯鐢╩ap.connect鎻愪緵緇欎綘.