顯示具有 rails view 標籤的文章。 顯示所有文章
顯示具有 rails view 標籤的文章。 顯示所有文章

2008年6月10日 星期二

partial

include the view in other file

ex:
render :partial=> "books"
render the view in _books.html.erb (  the partial view file must 
start with _ )

ex:
render :partial => "rabbit/list"
render the view in different directory

note: the variable partial view can access is the variable the master view can access 

2008年5月12日 星期一

paginate

divide large collections into pages

ex:
in the controller:
def  car_list
     @car_pages,  @cars = paginate ( :cars,  :order_by => 'name')
end
@car_pages is a paginator object.
@cars contain 10 rows.  ( default is 10 rows)

in the view:
table
  for car in @cars
       tr  td  car.name  td  tr
  end
table
hr
   pagination_links(@car_pages) 
hr
pagination_links  show the current page, two page numbers on either side of current page, first page & last page 

2008年5月11日 星期日

helper

remove the logic from view to helper

ex:
module   TestHelper
              def  run
                       "test_title"
              end
end

in the test controller
<%=  run  %>

originally the function in the helper file can only be accessed by related view,
but there is method for sharing helper

sharing helper:
method 1:
define the method in application_helper.rb

method  2:
ex:
add  TestHelper  to controller
helper  :test

h( )

escape html tag
ex:
<%= "<b>test</b>"% >
show test in bold
<%= h("<b>test</b>") %>
show  <b%>test</b>

xml template

builder template:

xml builder:
xml.tagName

ex:
xml.productName ( "car")
-->   <productName>car</productName>

cache

the page is cached .
Hence,  after first request, the same URL's response is deliver from cache, rails is no involved 

3 caches:
page caching,  action caching  & fragment caching

page caching:
ex:
caches_page  :test

action caching:
execute filter before return cached pages
ex:
before_filter  :test_filter
caches_action  :test2

turn on caching( in environment.rb) :
ActionController::Base.perform_caching = true


removing cache:
expire_page
ex:
expire_page  :action=>test

expire_action:
ex:
expire_action :action=>test2

sweeper:
observe the model. When the model is changed, the sweeper expire the cached pages that 
depend on the model

ex:
in app/models
class  TestSweeper <>
         observe  Car
         def  after_create(car)
                   expire_car_page
         end
end

in app/controllers
class Test <>
    cache_sweeper  :test_sweeper
end

the location to store cached pages:
ex:
http://localhost:3000/content/show/1  is stored in app/public/content/show/1.html




2008年5月10日 星期六

redirect_to

three forms:

1. redirect_to ( options ... )
ex:
redirect_to (:action=>jump)

2. redirect_to (path)
ex:
redirect_to ("/test/run")
show /view/test/run.html.erb

3. redirect_to( url)
ex:
redirect_to("http://www.google.com")

2008年5月8日 星期四

flash

the value stored in the flash in the first request is available in the second request.
After processing 2nd request, the value is removed from flash

main use:
error message

ex:
def  test
    flash[:note] = "error"
    redirect_to :action=>test2
end

test2.html.erb
<%= @flash[:note] %>

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

form helper

form_tag:   和model無關
ex:
<% form_tag do %>
<p><label for=”login”>Login</label>
<%= text_field_tag  ‘login’ %></p>
<p><%= submit_tag ‘Log in’ %> </p>
<% end %>

1.如果form_tag後沒有接action,那麼action就是form目前所在的
  頁面對應到的method
2存取使用者輸入的資料,例如param, such as params[:login], log對應
  到text_filed_tag的值

ex:
<% form_tag  :action=>'play'  do %>
<p><label for=”login”>Login</label>
<%= text_field_tag  'car',  ‘login’ %></p>
<p><%= submit_tag ‘Log in’ %> </p>
<% end %>
1. text_field_tag後接的car是model name,login是car的attribute
   , the controller can access it using  params[:car][:login]


submit_tag 後接的字串是submit button上的文字

form_for : 和model有關
ex:
 <% form_for :person, @person, 
    :url => { :action => "create" } do |f| %>
      <%= f.text_field :first_name %>
      <%= f.text_field :last_name %>
      <%= submit_tag 'Create' %>
  <% end %>

對應的html
 <form action="/persons/create" method="post">
      <input id="person_first_name" name="person[first_name]" size="30" type="text" />
      <input id="person_last_name" name="person[last_name]" size="30" type="text" />
    <input name="commit" type="submit" value="Create" />
    </form>

存取使用者輸入的資料:
以上面為例子,Person.new(params[:person] )

ex:
<% form_for :rabbit ,  url=>{ :controller=>:rabbit, :action=>create},
      :html=> {:id=>’peter’ } do |t| %>
對應的html
<form action="/rabbit/create" id="peter" method="post">

text_field & text_field_tag
regular form helper is usually end with tag
model form helper is usually not end with tag 
ex:
text_field :customer, :name
create customer in the action of controller:
@customer = Customer.new(params[:customer])

text_field:
hidden_field:
password_field:
text_area:
radio_button(:variable, :attribute,  tag_value,  options):
check_box:


select:
selection list
ex:



2008年5月6日 星期二

link

link to css
stylesheet_link_tag   'test'
test.css is in /public /stylesheets

link to image
image_tag  'test.jpg'
test.jpg is in  /public/images

link to page
link_to  "go to" ,  :controller=>"test", :action=>"index"

link to javascript:
javascript_indclude_tag  :defaults
link to prototype, effects, dragdrop, controls, application in /public/javascripts

url_for:
generate url according to route.rb
ex:
url_for ( :controller=>"rabbit", :action=>jump )
-->  http://localhost:3000/rabbit/jump 

layout

the layout for all website:
method 1:
use application.html.erb, it is the layout for all website
method 2:
ex:
define the layout test.html.erb
add following line to app/controllers/application.rb
layout "test"

yield:
render the template.  The template is rendered at this location within layout

layout can access variable defined in the template