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

2008年5月14日 星期三

the data controller can access

CGI data:
access @params  via params method
ex:
params[:id],  params[:user][:age]

session information:
access @session via session method
ex:
session['user']

model's data
ex: 
model Car has a name field
car is an instance of model Car
car.name


2008年5月11日 星期日

verification

verify that certain conditions are met

verify:
ex:
verify   :session => :user_id,
             :add_flash => { :note=> "you are wrong" }
             :redirect_to => index
if the session does not contain key :user_id,  
a note is added to flash & request is redirected to index

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")

group controllers into module

For example,  two directories  Test1  & Test2,
controller Rabbit1Controller in Test1,  Rabbit2Controller in Test2

url_for( :controller=> "rabbit1", :action=>"jump")
--->  http://localhost:3000/test1/rabbit1/jump

2008年5月8日 星期四

session & cookie

save & save!

save object into database

save! will raise a exception if the save is not successful 
save only return false when save is not successful 

2008年5月7日 星期三

filter

define the actions to do before(after) some actions

two define syntaxes:
1. use method to define filter
2. use class to define filter

before_filter:
define the actions to do before some actions
ex:
before_filter :check
do check before any action. 
If check return false,  the action is not done

after_filter:
define the actions to do after some actions

filter & inheritance :
B extends A, then A's filter is done first, then B's filter is done

filter's scope:
only for some actions:
ex:
before_filter :check,  :only=>:index
before_filter :check,  :only=>[:index,  :new]

not for some actions:
before_filter :check,  :except=>:index

around_filter:
the combination of before_filter & after_filter
ex:
class TestFilter
    def  before(controller)
         @name="peter"
    end

    def  after(controller)
         @name ="andy"
    end
end

around filter  TestFilter.new

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