【转】深刻理解render 和 redirect_to
2024-10-21 19:26:46
由于最近老是在表单提交后出现没有反应的现象,发现是在action中的使用render 和 redirect_to的原因,于是就想搞清楚他两真正的区别在哪里,上一遍的blog也谈到了这二者的区别,但是有点浅,
http://www.blogjava.net/fl1429/archive/2009/03/10/258886.html
下面从我们的程序实验开始:
1,建立controller
test_controller.rb
class TestController < ApplicationController def test1
puts "test1A"
render :action => "test1"
puts "test1B"
end def test2
puts "test2A"
redirect_to :action => "test1"
puts "test2B"
end def test3
puts "test3A"
redirect_to :action => "test3"
puts "test3B"
end end
2,建立view
在对应的views->test目录下有test1.rhtml,test2.rhtml,test3.rhtml,内容随便写,例如内容都为 hello word
3,启动webrick
到相应的目录下Ruby script/server
4,浏览器中浏览页面
(1)页面test1.rhtml: http://localhost:3000/test/test1
浏览器中直接输入地址结果是:
可能是:
1test1A
2test1B
3 127.0.0.1 - - [12/Mar/2009:18:10:11 中国标准时间] "GET /test/test1 HTTP/1.1" 304 0 - -> /test/test1
也可能是:
1127.0.0.1 - - [12/Mar/2009:18:29:50 中国标准时间] "GET /test/test1 HTTP/1.1" 304 0 - -> /test/test1
2test1A
3test1B
(2)页面: test2.rhtml http://localhost:3000/test/test2
结果:
1test2A
2test2B
3127.0.0.1 - - [12/Mar/2009:18:11:10 中国标准时间] "GET /test/test2 HTTP/1.1" 302 98 - -> /test/test2 127.0.0.1 - - [12/Mar/2009:18:11:10 中国标准时间] "GET /test/test1 HTTP/1.1" 304 0 - -> /test/test1
4test1A
5test1B
还可以发现最后,浏览器的地址的变为: http://localhost:3000/test/test1
(3)页面test3.rhtml http://localhost:3000/test/test3
1test3A
2test3B
3127.0.0.1 - - [12/Mar/2009:18:12:29 中国标准时间] "GET /test/test3 HTTP/1.1" 302 98 - -> /test/test3
4test3A
5test3B
6127.0.0.1 - - [12/Mar/2009:18:12:29 中国标准时间] "GET /test/test3 HTTP/1.1" 302 98 - -> /test/test3
执行效果是死循环.
由上述实验得到结论:
1,无论是render 还是 redirect_to 都是方法体内的内容全部执行完再跳转,就算跳转了,方法体内的还是会全部执行的
2,render 是跳转到对应的view下rhtml
3,redirect_to 是跳转到对应的 action 里,所以页面三执行的效果是死循环!
最新文章
- windows 部署 git 服务器报 Please make sure you have the correct access rights and the repository exists.错误
- a版本十日冲刺总汇
- How threads differ from processes
- Mysql忘记密码修改密码
- office2013 win 32bit (含激活工具)
- SpringMVC ResponseBody返回中文乱码解决方案
- OpenCV在Debug和Release两种模式下布恩那个同时运行的问题
- wordpress主题制作常用基本的模板及说明
- SKScene类
- js 懒加载
- MyEclipse破解方法
- mongodb group包(最具体的、最受欢迎、最容易理解的解释)
- LuaBridge 中C++类和继承示例
- [SQL基础教程] 2-1 SELECT语句基础
- Spring AOP 整理笔记
- Cordova入门系列(三)Cordova插件调用 转发 https://www.cnblogs.com/lishuxue/p/6018416.html
- [算法] 举一反三之n重复数组中找唯一m重复异类数
- git 合并冲突 取消合并
- 《Java从入门到精通》学习总结2
- leetcode — anagrams
热门文章
- 我的Java开发学习之旅------>Base64的编码思想以及Java实现
- genymotion device manager列表没有
- 1.搭建Django开发环境
- 第13条:合理利用try/expect/else/finally结构中的每个代码块
- Vue组件通信(传值)
- htmlParser的使用-链接
- 删除power by dedecms的方法
- mysql 索引技巧
- In a Web Application and Mobile (hybrid), how to know which this platform running?
- jquery 实现智能炫酷的翻页相册效果