神剑山庄资源网 Design By www.hcban.com
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,rails,页面跳转
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无Ruby rails 页面跳转(render和redirect_to)的评论...
更新日志
2024年10月04日
2024年10月04日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】