1、定URL配置文件地址:config\routes.rb
例1:map.connect 'aaa/:action/:id',:controller=>'site'
说明,http://localhost:3000/aaa/index/1,中aaa实际指向site
例2:map.connect ':action/:id',:controller=>'site'
说明:http://localhost:3000/index/1 虽然没有指定控制器,但实际向site。
例3:map.connect ':id',:controller=>'site',:action=>'index'
说明:http://localhost:3000/1,将实际指向site控制器的index Action
另外“:requirements => {:id => /^[0-9]+$/}”这样设置可以将id部份值限为数字
除了:controller、:action参还:year:month:day表示年月日的参数
用url_for方法可以生成URL,它必须符合map.connect中义url_for(:controller=>”site”,:action=>”show”, :id=>111)
2、hide_action声明action方法为private或protected型
例:hide_action :some_action 则some_action为private型
3、redirect_to此方法用于action间的跳转
例:
redirect_to :action=>"edit", :id=>7
redirect_to "http://www.chengang.com.cn"
redirect_to "/images/1.jpg" #到../image/1.jpg
redirect_to :backk #返回上次访问页3.2.3.render显输出给户(·21·
4、render
render :text=>'HelloWorld!!'#输出“HelloWorld“
render(:action=>:index) 调用父类的index()方法并显示其结果。
render(:template=>"test/index") 显示index.rhtml模板
render(:file=>”F:/index.rhtml”) 显示指定文件
render(:file=>”test/index”, :user_full_path=>true)显示文件,参数2表相对路径
render(:nothing=>true) 一个空页面
render :inline=>”<%='Hello' + str %>”,:locals=>{:str=>”World”}, :type=>”rhtml”
显示一个动态模板,参数1是模板代码,参数2是模板可用的变量值,参数2是模板类型(or rxml)
5、render_to_string
和render用法相同,但本方法返回值是字符串类型
6、send_data
向客户端发送二进制数据流
例:
img=Photo.find(:first)
send_data(img,type=>"image/jpeg",:disposition=>"inline")
参数1可以指定一个文件名,参数2指定所传送的数据类型,参数3为浏览器对数据的处理方式:值为inline时,直接显示数据,为attachment时,下载并保存相关数据
7、send_file
本方法和send_data类似,但它更适合大数据量的传输。除了有send_data的三个参数处,本方法还有两个参数,
:streaming=>true(数据向边读边传,默认为true,为false时,表示将文件全部读入服务器内存后在向用户发送文件)
:buffer_size=>4096数据缓冲区大小(一次传输的数据量,4096字节是默认值)
8、flash(action方法间的通信器)
action之能的变量是不能互访的,带@前缀的实变量也不行。就是flash的有效期也只能在下一个request之间有效,再过一个Request就失效。实例变量代替不了flash的,因为IE无状特性,在下一个请求中,上一个请求的实例变量已失效。
flash[:myname]= ‘chengang’ 存值,输出为puts flash[:myname]
flash.keep(:myname)可以保证myname的值不被修改。即使该来自上一个action,用keep之后,它就可以再传到下一个action
flash.now[:myname] = “chengang” now和keep相反,它指定的值只在当前action有效
例1:map.connect 'aaa/:action/:id',:controller=>'site'
说明,http://localhost:3000/aaa/index/1,中aaa实际指向site
例2:map.connect ':action/:id',:controller=>'site'
说明:http://localhost:3000/index/1 虽然没有指定控制器,但实际向site。
例3:map.connect ':id',:controller=>'site',:action=>'index'
说明:http://localhost:3000/1,将实际指向site控制器的index Action
另外“:requirements => {:id => /^[0-9]+$/}”这样设置可以将id部份值限为数字
除了:controller、:action参还:year:month:day表示年月日的参数
用url_for方法可以生成URL,它必须符合map.connect中义url_for(:controller=>”site”,:action=>”show”, :id=>111)
2、hide_action声明action方法为private或protected型
例:hide_action :some_action 则some_action为private型
3、redirect_to此方法用于action间的跳转
例:
redirect_to :action=>"edit", :id=>7
redirect_to "http://www.chengang.com.cn"
redirect_to "/images/1.jpg" #到../image/1.jpg
redirect_to :backk #返回上次访问页3.2.3.render显输出给户(·21·
4、render
render :text=>'HelloWorld!!'#输出“HelloWorld“
render(:action=>:index) 调用父类的index()方法并显示其结果。
render(:template=>"test/index") 显示index.rhtml模板
render(:file=>”F:/index.rhtml”) 显示指定文件
render(:file=>”test/index”, :user_full_path=>true)显示文件,参数2表相对路径
render(:nothing=>true) 一个空页面
render :inline=>”<%='Hello' + str %>”,:locals=>{:str=>”World”}, :type=>”rhtml”
显示一个动态模板,参数1是模板代码,参数2是模板可用的变量值,参数2是模板类型(or rxml)
5、render_to_string
和render用法相同,但本方法返回值是字符串类型
6、send_data
向客户端发送二进制数据流
例:
img=Photo.find(:first)
send_data(img,type=>"image/jpeg",:disposition=>"inline")
参数1可以指定一个文件名,参数2指定所传送的数据类型,参数3为浏览器对数据的处理方式:值为inline时,直接显示数据,为attachment时,下载并保存相关数据
7、send_file
本方法和send_data类似,但它更适合大数据量的传输。除了有send_data的三个参数处,本方法还有两个参数,
:streaming=>true(数据向边读边传,默认为true,为false时,表示将文件全部读入服务器内存后在向用户发送文件)
:buffer_size=>4096数据缓冲区大小(一次传输的数据量,4096字节是默认值)
8、flash(action方法间的通信器)
action之能的变量是不能互访的,带@前缀的实变量也不行。就是flash的有效期也只能在下一个request之间有效,再过一个Request就失效。实例变量代替不了flash的,因为IE无状特性,在下一个请求中,上一个请求的实例变量已失效。
flash[:myname]= ‘chengang’ 存值,输出为puts flash[:myname]
flash.keep(:myname)可以保证myname的值不被修改。即使该来自上一个action,用keep之后,它就可以再传到下一个action
flash.now[:myname] = “chengang” now和keep相反,它指定的值只在当前action有效