页面跳转之 redirect_to &render

redirect_to实现的是action方法的跳转,向浏览器发起一个新的请求,具体使用方法如下:
 
redirect_to :action => 'edit', :id => 7 
redirect_to "http://wiisola.javaeye.com/" 
redirect_to "/images/1.jpg" 
redirect_to :back 
------------------------------
1. Redirect to Show, flash is shown, works
      format.html {
           redirect_to(@article, :notice => 'Article was successfully
saved.')
        }


2. Redirect to Edit, flash is shown, works
        format.html {
          flash[:notice] = 'Article was successfully saved.'
          redirect_to :action => 'edit' 
        }
---------------------------------
其中第4行是回到上一次访问的页面。 
render可以翻译成"渲染",也就是说,render仅仅渲染了一个新的模板,而没有执行相应的action。render的用法如下: 

render(:text => string) 
render(:inline => string, [:type => "rhtml"|"rxml"]) 
render(:action => action_name) 
render(:file => path, [:use_full_path => true|false]) 
render(:template => name) 
render(:partial => name) 
render(:nothing=>true) 
render()

第1行:直接渲染出文本 
第2行:把传入的string渲染成模板(rhtml或者rxml) 
第3行:直接调用某个action的模板,相当于forward到一个view 
第4行:使用某个模板文件render, 当use_full_path参数为true时可以传入相对路径 
第5行:使用模板名render,e.x.: render(:template => "blog/short_list") 
第6行:以局部模板渲染 
第7行:什么也不输出,包括layout 
第8行:默认的的render, 相当于render(:action => self) 
发布了23 篇原创文章 · 获赞 7 · 访问量 12万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章