Ruby rails 页面跳转(render和redirect_to)
更新时间:2009年05月17日 16:34:27 作者:
今天在做R.R.log的时候发现个问题,在修改密码的时候如果没有通过校验,没有显示校验错误的信息。
Ruby代码
if @user.update_attributes(:password => params[:user][:password])
flash[:notice] = '密码修改完成'
redirect_to :action => 'index'
else
redirect_to :action => 'change_pass', :id => @user
end
后来随手改了下第5行,把redirect_to改为render,居然就OK了。网上找了下才发现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
其中第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)
补上一个手动render的例子:
Ruby代码
def search
@results =Search.find(params[:query])
case @results
when 0 then render :action=> "no_results"
when 1 then render :action=> "show"
when 2..10 then render :action=> "show_many"
end
end
def search
@results =Search.find(params[:query])
case @results
when 0 then render :action=> "no_results"
when 1 then render :action=> "show"
when 2..10 then render :action=> "show_many"
end
end
但是我自己的问题仍然没有解决,为什么用render渲染一个模板能够显示错误信息,但用redirect_to重新请求就没有呢?也许看源码能够解决吧,可惜看不懂,汗..总之以后记住render和redirect_to的用法就是了。
复制代码 代码如下:
if @user.update_attributes(:password => params[:user][:password])
flash[:notice] = '密码修改完成'
redirect_to :action => 'index'
else
redirect_to :action => 'change_pass', :id => @user
end
后来随手改了下第5行,把redirect_to改为render,居然就OK了。网上找了下才发现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
其中第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)
补上一个手动render的例子:
Ruby代码
复制代码 代码如下:
def search
@results =Search.find(params[:query])
case @results
when 0 then render :action=> "no_results"
when 1 then render :action=> "show"
when 2..10 then render :action=> "show_many"
end
end
def search
@results =Search.find(params[:query])
case @results
when 0 then render :action=> "no_results"
when 1 then render :action=> "show"
when 2..10 then render :action=> "show_many"
end
end
但是我自己的问题仍然没有解决,为什么用render渲染一个模板能够显示错误信息,但用redirect_to重新请求就没有呢?也许看源码能够解决吧,可惜看不懂,汗..总之以后记住render和redirect_to的用法就是了。
您可能感兴趣的文章:
- Ruby学习笔记之gem 命令详解
- Ruby 字符串处理
- RUBY 新手教程 跟我一起学ruby
- 学习Ruby你需要了解的相关知识(rvm, gem, bundle, rake, rails等)
- Ruby中执行Linux shell命令的六种方法详解
- 淘宝网提供的国内RubyGems镜像简介和使用方法
- 二十分钟 教你Ruby快速入门 图文教程
- 详解Ruby中正则表达式对字符串的匹配和替换操作
- Ruby Gems更换淘宝源方法
- Windows下Ruby on Rails开发环境安装配置图文教程
- ruby 学习笔记(2) 类的基本使用
- ruby 异常处理:rescue
- CentOS 6.3下编译安装Ruby 2.0笔记
- Ruby中的return、break、next详解
- 举例讲解Ruby中require的使用方法
- 更改RubyGem安装源
- 使用Ruby来处理JSON的简单教程
- Ruby信号处理详解
相关文章
ruby、javascript、php中的观察者模式实现代码
这篇文章主要介绍了ruby、javascript、php中的观察者模式实现代码,通过本文可以深入的理解观察者模式,需要的朋友可以参考下2014-11-11
最新评论