关于作者

相关热点

rails-3

上一篇 / 下一篇  2008-05-05 21:26:46

URL生成
*~8j*S#q's0使用url_for()在线教育资讯@A2p.IFmX6T*E.Z
# No action or id, the rewrite uses the defaults在线教育资讯Bc6o7dB-u!_a?Ae
url_for(:controller => "store" )在线教育资讯2NIli6x
#=> http://pragprog.com/store
dW;n#TAM W0# If the action is missing, the rewrite inserts在线教育资讯T:@v O3T$@5Kw2qo
# the default (index) in the URL在线教育资讯6p @2Ia.aYi"[x
url_for(:controller => "store" , :id => 123)
:D!V _3Yif5S#K0#=> http://pragprog.com/store/index/123在线教育资讯sl+Q/R$L9}.N
# The id is optional
%NFQ+~iLI'H!]0url_for(:controller => "store" , :action => "list" )在线教育资讯5O7i`f9d^ _6M*K }!u
#=> http://pragprog.com/store/list在线教育资讯B_'M7g(B5}
# A complete request在线教育资讯a&g"Y#nHS
url_for(:controller => "store" , :action => "list" , :id => 123)
0_^X;Yo2U0#=> http://pragprog.com/store/list/123
(?{8`7LX%V%@:|E4y:^ o0# Additional parameters are added to the end of the URL
[[]m'gE J0url_for(:controller => "store" , :action => "list" ,
(d%fUr#LT E0:id => 123, :extra => "wibble" )在线教育资讯*|C9f,~h
#=> http://rubygarden.org/store/list/123?extra=wibble
l/x)Ig:G3G0
zmWj]1V6b0在线教育资讯"B8}LNP,J o'a.B!i
map映射认为url是一个hierarchy,如果改变层级的上层,rails会停止揣测下层的值在线教育资讯#FAf9F\j A*U
url_for(:year => "2004" )
tPRl4_$q yIT6q0#=> http://pragprog.com/blog/2004
*kV$F1z?)V0后面的月和日就没有了,底层值只有在上层值不变的时候才有存在的意义
'w9ZDY{'bqbw6^0
5E2a#Hb~4G0如原来的url是http://pragprog.com/blog/2005/4/18在线教育资讯;?ml e6_s!D
url_for(:day => "25" )在线教育资讯o#ap@9k ? k
#=> http://pragprog.com/blog/2005/4/25在线教育资讯WWB,f6OP5fnM

'D6}/O"Z*]0:anchor string  锚点在线教育资讯2w;G\3Xb)L"R%K
:host string    host name and port in the URL. 在线教育资讯6o.p f&Z"Ue!m1z
:only_path boolean 仅生成路径,省略 the protocol,host name, and port .在线教育资讯6r2\/^b'ist(sG
:protocol string 设定协议. Use a string such as "https://".
??*jI/I$_L0:trailing_slash boolean Appends a slash to the generated 在线教育资讯 V9|"g]or e

bEz(iPJ1K0Name Routes
8D7W6\FPk Z0如果在route.rb中是这样写的话
(}!?#i d }$I'iU0ActionController::Routing::Routes.draw do |map|在线教育资讯 n2s:s:~ F@;lPr
# Straight 'http://my.app/blog/' displays the index在线教育资讯 vN[m(Xa
map.index "blog/" ,
I3l,R z0E@C }K0    :controller => "blog" ,在线教育资讯4B;}XMfV!Q
    :action => "index"
'y'n.Pf3D@6a7{0# Return articles for a year, year/month, or year/month/day在线教育资讯k"c };W4pc
map.date "blog/:year/:month/:day" ,在线教育资讯T G%u+s8]L
    :controller => "blog" ,
Pq4F1bZ3yD0    :action => "show_date" ,在线教育资讯[K)E+mm/H Hhf
    :requirements => { :year => /(19|20)\d\d/,
!Y Iiyzo0T(y(a0                                            :month => /[01]?\d/,在线教育资讯q6F9{OW0uJ
                                            :day => /[0-3]?\d/},
a*s*_p&@["?k}0                                            :day => nil,在线教育资讯},EHl8\I
    :month => nil
3n#Sh.u9`:i} `0Q)|2}0# Show an article identified by an id在线教育资讯 |X v xhS
map.show "blog/show/:id" ,
r0H)GV7`$Q]t0    :controller => "blog" ,在线教育资讯4I$^4M.r8O VX RB
    :action => "show" ,在线教育资讯 j g[ i$KoazG
    :id => /\d+/
o.`+_3z0kFR\0# Regular Rails routing for admin stuff
0eM.V r(QM0map.admin "blog/:controller/:action/:id"在线教育资讯?6CMPY:S5?#t,P
# Catch-all so we can gracefully handle badly-formed requests在线教育资讯9e @?*X9A6d
map.catchall "*anything" ,
$Y/J,K k2nOk5h6oA0    :controller => "blog" ,
|^}'XQ+UT0    :action => "unknown_request"在线教育资讯/l aH A$Q9j3],g
end在线教育资讯,A F)J4_M@%D`

^6t!W V0_7~ G"|0根据命名规则
5TI B:txH B-J]0index_url在线教育资讯lJ'mP6[1X
#=> http://pragprog.com/blog
?;_I+L i9K-ni0date_url(:year => 2005)在线教育资讯7B'EHZ5d'F
#=> http://pragprog.com/blog/2005在线教育资讯K C&[cy'D
date_url(:year => 2003, :month => 2)在线教育资讯PVnXu
#=> http://pragprog.com/blog/2003/2
m`w/W;W}*p0show_url(:id => 123)在线教育资讯 qe!IU$h
#=> http://pragprog.com/blog/show/123
#p{vS%]0在线教育资讯IAWY%B TvL8r
我们在程序中可这样使用在线教育资讯R{Qg*_["n A
redirect_to(index_url)在线教育资讯2TU2v {)]RQ
或 <%= link_to("Index" , index_url) %>在线教育资讯!uE!o.vktB1] x(g
在线教育资讯b8?*R(m A+R.LW Gk

O%?/QZ!`/[@6@5H018.4 Action Methods在线教育资讯6y.P*B%k5E2Aq
Controllor中的所有public的方法都可以被当作url直接调用,为了避免不安全因素,仅曝露需要的方法。其它方法定义为protected或private。也可以用 hide_action()
V$^dh&Tao0如 hide_action :check_credit
aF u'U%tC}0但是,这不是一个好的解决方法,应该考虑用helper来代替在线教育资讯n/xdTk dV_&k
----------------------------------------------------------
)Ji rBi} L0H0Controller Environment
\c_k6hDA0request对象在线教育资讯8F]0er7~TN3v
    domain() 返回request的域名最后两个部分,如XXX.com, XXX.org
P3in _)m p5X3]Y+kw u8g0    remote_ip() 返回远端的ip地址在线教育资讯0ER exBN
    env() 返回环境参数在线教育资讯 v$f7B*z}5m1M
    method()  :delete, :get, :head, :post, :put
1bvg]IfJ0    delete?, get?, head?, post?, put?
)z;T#o'O3P`2g+aU0
tbH"Ay1~"k0params对象在线教育资讯kx:ghVZ2V'\:jK
    返回一个哈希表,包含request parameters,可以是一个symbol或字符串,如params[:id]或params['id']
7|OxR3Y Q?0
$xb(}-lh3{ onG0cookies 对象在线教育资讯8z)X'ojh9no
    返回哈希表
3cI LA%F t"y0
&|(N\ Iq gl\0session对象
w9{7l1z NE)E0    返回哈希表
A,`fUs `D0在线教育资讯Ak?@1P AV@Z,N
response对象在线教育资讯;B3l"xQ wZ%p%D6]J
    
4OG$Fc`:|0headers对象在线教育资讯9N_ bMo)HAF'qp$n
    返回head相关的哈希表,
M+B%[ a [*^/_tL.t0
5N&E5Q|*B@1u"N5F0--------------------------------
*f%x;t6e'S0Responding to the user
L6]Vj-@H_0controller的作用之一就是向用户反馈,最基本的有三种途径
vikv*D r\rx7~01. 渲染一个模板
@xzv#?-]8uN02. 直接反馈一个字符串,而不激活任何view,一般用来报错在线教育资讯m#UTM#R6I
3. 发送其他数据到客户端,如下载文件在线教育资讯K y*uL`[ _b
在线教育资讯J*}X s-R!zz
可使用 render() or send_xxx()来处理反馈在线教育资讯P:E](?9|]"@hQ
如果被render两次就会有doubleRenderError错,可用 erase_render_results()来擦除上次render的结果,直接使用后一次render
T%q@8Is5s0在线教育资讯"NF2w7_#G0~q
--------------------------------
'^k2kE3zrr'oC9x ])}0Rendering Templates
;whvW'{R-o0两种模板  rhtml在线教育资讯,\i*^.F+zpA
                    builder在线教育资讯/g#uoE2p"q6HB
在线教育资讯 ^o@.R4sw x
默认情况模板在 app/views/下,可以通过配置更改
/~$f ?-]*Y b lyj0    ActionController::Base.template_root = dir_path
8y g|A3__q0
qv-PMAL0render()方法是渲染的核心函数在线教育资讯,T.b1x3k.S1T|2[H
在线教育资讯&P({\B,}RC7Gz w(k;tw
render(:text =>string) 直接将文本发送到浏览器
v9W2vB&|w6{|*P0class HappyController < ApplicationController
G'm W:r1NbY o1ui0    def index在线教育资讯X N qC(Q-B
        render(:text => "Hello there!" )在线教育资讯2m,ce qr-feTS
    end在线教育资讯G y+m P&d+V"s9k8V u
end
V1{ a b]0
{{,w-h%m6p"m0render(:inline =>string, [ :type =>"rhtml"|"rxml" ] )
#wmO,Tx,Mc0    inline渲染在线教育资讯2yjleBM0w
class SomeController < ApplicationController
i)T;gI'{SQ.B]*n0    if RAILS_ENV == "development"
B2^y*d:C0        def method_missing(name, *args)
r)z1a o2|;^W0            render(:inline => %{在线教育资讯+d%BGm)K ye5O!F
            

Unknown action: #{name}

在线教育资讯2G5j0V9xm
            Here are the request parameters:
_4Y(I],td3_0
6g _!{%U v:[ Z:QJc4J0            <%= debug(params) %> })
F*\i%Bc { q8b0        end在线教育资讯Z6o r8Z6M8B'J#G
    end在线教育资讯5b/b~d&I
end
p#q;}a0W2_y"tH0
,px7}v'sk1GM/d0在线教育资讯5pA;e DJb
render(:action =>action_name)
5hB"q(N*lYx0    渲染一个action在线教育资讯9qu~;` |
def display_cart
r3VOX$N ~DoQ X/E0    if @cart.empty?
@"_ FTX#G0        render(:action => :index)
9fIQ dk ZE%q3y9|6b0    else
dy*AZ^-Q-E5s%Ts0        #。。。在线教育资讯 ?6b2Kg5F:Jl
end
z6C'z U4D rA0在线教育资讯~9T&y.H v
render(:file =>path, [ :use_full_path =>true|false] )在线教育资讯 p%bt9X*V(pe+~
    Renders the template in the given path
3^*X`jrJ0在线教育资讯(X;ZPb0d&t2CE
render(:template =>name)
/g`y2[:a/f }$c0    Renders a template and arranges for the resulting text to be sent back to the client.在线教育资讯[nm9I_%OG4S
在线教育资讯f J%ral?[S
render(:partial =>name, ...)
\ jN i;N0在线教育资讯zw4p6{DG-N z[
render()
u)q~k BThNN0    默认渲染本action,可省略在线教育资讯^6{R!VWj'P
    def index
X1P:J ?o6r[0        render
*g-G E(Tu3gxE6`'T*p s0    end在线教育资讯h8e)zd:K)dj#W,w
在线教育资讯"Kb*N,fk|3j7I6dHq l
所有的render都可以带 :status和:layout参数
1] ^BH:xNTGXLS0    :status设定http response,默认为200 OK在线教育资讯 k yjYQ+Q8R
    :layout设定在render时候是否使用layout
$`$P,@+_/R*C9L0---------------------------------------------
K+Z&R,d!x.tN0Sending Files and Other Data在线教育资讯,n3ZQfU%WY

+k9te-Lan*yG;~0{0send_data()
c#ePG)Fo0    Send a string containing binary data to the client.在线教育资讯6L%r*i/@;d)U%E5@,Rc
def sales_graph在线教育资讯^$]Y&v7`Bd;U"t
    png_data = Sales.plot_for(Date.today.month)
.k o!v0E$oM4M;_D`0    send_data(png_data, :type => "image/png" , :disposition => "inline" )在线教育资讯oi9Le`:\0M9Vm5z
end在线教育资讯 q5hNwqpz
在线教育资讯N bM3]$J#hj*I8g#o
在线教育资讯U h`%VM7\
send_file
D#qp _L}yDy0    Send the contents of a file to the client.在线教育资讯&X6U obe3{-_sY&k dv
def send_secret_file在线教育资讯[ CG.| ue!L?
    send_file("/files/secret_list" )在线教育资讯4^pY@ h^I1Qb
    headers["Content-Description" ] = "Top secret"
L8D)~T,w4R#q0TM(i0end在线教育资讯3SN_F5x
在线教育资讯&c|0~j8V#|Q+~
-----------------------------------------------
k+V7g)d!Ee ^;L0重定向在线教育资讯-j#UKk-`1v$p&D

6xB3W/^H E:hVZ+W0redirect_to(path)
5y9u5ZIy1bN0redirect_to(url)在线教育资讯ASAF#cU_t
在线教育资讯,tI l+b4r?'lh_
--------------------------------------------在线教育资讯0VN b_n1Q&{*S
18.5 Cookies and Sessions
/zrx+t:Cb-ji0cookie的值必须是string
Bm4g|8C0浏览器在保存cookie的时候还可以保存一些其他信息
seS(L,P0如 :domain, :expires, :path, :secure(仅在https的时候返回cookie)在线教育资讯C%h{.pP|OVS uw
cookies[:marsupial] = { :value => "wombat" ,
*lw;j4tv8g ?0                                                :expires => 30.days.from_now,在线教育资讯 dk"\:_\M
                                                :path => "/store" }在线教育资讯)[%G{uSDB4g
因为有点浏览器不支持cookie或有人设定不接受cookie所以在使用cookie的时候要考虑浏览器不接受cookie的情况
W7P(\n4ZS0
F"^&|*tqZ"^Q0-----------------------------------在线教育资讯| v/cU+BP:s.KZ
rails sessions
在线教育资讯{)jSh-P%W)r

Mm'w3Bo$z0Link URL: http://blog.163.com/zjliqing@126/blog/static/39161019200791610515288

TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)