2008年5月8日 星期四

controller action, view tempelate, render

controller裡定義的action(method)會對應到view下的templelate
例如index對應到index.html.erb
所以如果controller要做的action是index,
browser顯示的網頁即是index.html.erb

為何action是index,即顯示index.html.erb ?
一般在controller定義的action,
例如index,
預設都會呼叫如下的render 指令,
所以controller在做index action時,
即會顯示index.html.erb

render command:
ex:
render :template => “test/index”
顯示 view/test/index.html.erb
:template後接的path是相對於view
如果改成:file,之後的path是絕對路徑

如果render顯示的是另一個page,
改變的是顯示的內容,
URL還是原page的URL,
若要連URL一起改變,
要代用redirect_to

ex:
render  :action=>”index”
顯示 index.html.erb
(注意render不會做action,只會顯示頁面,
若要做action,要用redirect_to) 

ex:
render :text=> “hello”
顯示hello

:status
ex:
render  :status=>404
html response的status

可以不定義action,但一定要定義顯示的頁面
例如index action沒定義,
但index.html.erb有定義,
則browser還是可以顯示正確的頁面。

the instance variable of the controller is available in the template
the template can access instance variable set in the controller action. Hence, rabbit's view can access dog's controller's instance variable if it is triggered from dog's action

沒有留言: