2007-07-03
Rails中使用REST,登录相关的问题,如何获得当前正在处理的url?
如果整个routes是使用传统的mvc方式实现的话,我们可以简单地记录一下当前的controller和action的名字,等到登录成功之后就可以redirect过去。
但是如果使用REST,则就不能简单地记录controller和action,例如/articles/1;edit这样的url就比较特别。另外一个问题是,除了要记录url外,还要记录使用什么HTTP Method,是GET, Post, PUT还是DELETE。
如果涉及到的action就是标准的index, create, new, show, update, edit, destory,那么可以使用一个case判断,然后调用相应的url helper,比如articles_url,然后把HTTP Method以硬编码的方式写入。但是如果在routes.rb里面添加了自己的routine,比如
那么又要怎么办呢?自己加入的routine有三种类型:collection, :member和:new,HTTP Method也可以随意指定,那么要怎么生成url呢?
但是如果使用REST,则就不能简单地记录controller和action,例如/articles/1;edit这样的url就比较特别。另外一个问题是,除了要记录url外,还要记录使用什么HTTP Method,是GET, Post, PUT还是DELETE。
如果涉及到的action就是标准的index, create, new, show, update, edit, destory,那么可以使用一个case判断,然后调用相应的url helper,比如articles_url,然后把HTTP Method以硬编码的方式写入。但是如果在routes.rb里面添加了自己的routine,比如
map.resource :articles, :collection => { :recent => :get }
那么又要怎么办呢?自己加入的routine有三种类型:collection, :member和:new,HTTP Method也可以随意指定,那么要怎么生成url呢?
评论
hideto
2007-07-03
1,使用request.request_uri类似的方法就可以记录当前url
2,使用request.method就可以记录当前:method
上面1和2在application.rb里的login逻辑里捕获即可
另外我觉得有时候完全用REST不是很现实,毕竟REST有一定的适用范围
2,使用request.method就可以记录当前:method
上面1和2在application.rb里的login逻辑里捕获即可
另外我觉得有时候完全用REST不是很现实,毕竟REST有一定的适用范围
AllenYoung
2007-07-03
hideto 写道
:collection -- add named routes for other actions that operate on the collection.
比如对于如下route:
生成的named route为rss_messages,生成的helper方法为rss_messages_path,url则为/messages;rss
该参数形式为#{action} => #{method}的hash,method为:get/:post/:put/:delete/:any,如果method无所谓则可以使用:any
:member -- same as :collection, but for actions that operate on a specific member.
即:collection参数为对多个对象操作的方法,:member参数则为对单个对象操作的方法
:new -- same as :collection, but for actions that operate on the new resource action.
比如对于如下route:
map.resources :messages, :collection => { :rss => :get }
生成的named route为rss_messages,生成的helper方法为rss_messages_path,url则为/messages;rss
该参数形式为#{action} => #{method}的hash,method为:get/:post/:put/:delete/:any,如果method无所谓则可以使用:any
:member -- same as :collection, but for actions that operate on a specific member.
即:collection参数为对多个对象操作的方法,:member参数则为对单个对象操作的方法
:new -- same as :collection, but for actions that operate on the new resource action.
我明白restful的routine的原理,问题是如何在代码中自动生成当前处理的url,我现在使用的代码如下:
# application.rb
def restful_url(options = {}, *parameters_for_method_reference)
restful_hash = Hash.new
# 先对7个标准的action进行处理
case options[:action]
when 'index'
restful_hash[:url] = self.send(options[:controller] << '_url')
restful_hash[:method] = :get
when 'create'
restful_hash[:url] = self.send(options[:controller] << '_url')
restful_hash[:method] = :post
when 'new'
restful_hash[:url] = self.send('new_' << options[:controller].singularize << '_url')
restful_hash[:method] = :get
when 'show'
restful_hash[:url] = self.send(options[:controller].singularize << '_url', options[:params])
restful_hash[:method] = :get
when 'update'
restful_hash[:url] = self.send(options[:controller].singularize << '_url', options[:params])
restful_hash[:method] = :put
when 'edit'
restful_hash[:url] = self.send('edit_' << options[:controller].singularize << '_url', options[:params])
restful_hash[:method] = :get
when 'destroy'
restful_hash[:url] = self.send(options[:controller].singularize << '_url', options[:params])
restful_hash[:method] = :delete
# 当是其他action时,使用传统的url_for
else
restful_hash[:url] = url_for(options, parameters_for_method_reference)
restful_hash[:method] = :get # 这里要怎么办?从哪里得到HTTP Method信息?
end
restful_hash
end
对以上方法的调用如下:
restful_hash = restful_url(:controller => controller_name, :action => action_name, :params => params) session[:intended_url] = restful_hash[:url] session[:intended_method] = restful_hash[:method]
但是使用我这种实现方式,每当有人添加了一个自己的routine时,都要来修改这个restful_url方法。我的意思是有没有什么办法更灵活一些。我这个方法已经考虑了不同controller的情况,因此适用于所有controller的标准action。同时也考虑了嵌套资源。
hideto 写道
有时间会去看看的,谢谢。
hideto
2007-07-03
:collection -- add named routes for other actions that operate on the collection.
比如对于如下route:
生成的named route为rss_messages,生成的helper方法为rss_messages_path,url则为/messages;rss
该参数形式为#{action} => #{method}的hash,method为:get/:post/:put/:delete/:any,如果method无所谓则可以使用:any
:member -- same as :collection, but for actions that operate on a specific member.
即:collection参数为对多个对象操作的方法,:member参数则为对单个对象操作的方法
:new -- same as :collection, but for actions that operate on the new resource action.
详见Rails源码研究之ActionController:八,resources
比如对于如下route:
map.resources :messages, :collection => { :rss => :get }
生成的named route为rss_messages,生成的helper方法为rss_messages_path,url则为/messages;rss
该参数形式为#{action} => #{method}的hash,method为:get/:post/:put/:delete/:any,如果method无所谓则可以使用:any
:member -- same as :collection, but for actions that operate on a specific member.
即:collection参数为对多个对象操作的方法,:member参数则为对单个对象操作的方法
:new -- same as :collection, but for actions that operate on the new resource action.
详见Rails源码研究之ActionController:八,resources
- 浏览: 142257 次
- 来自: 上海交通大学软件学院

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
另一只眼看Eclipse,所谓 ...
其他不管你学什么都会遇到一定的困惑的,一定的。
-- by mylvan -
我的第一关rake文件
robbin 写道每次当我想操起ruby写rake file的时候,都发现我三行 ...
-- by rubynroll -
我的第一关rake文件
抛出异常的爱 写道rake是建表结构的....不是用来导数据的 不如用exce ...
-- by liusong1111 -
我的第一关rake文件
不知大家有没有这种需求,用户的日常操作中,原始数据可能是其他人员发给他的exce ...
-- by zengyinbo -
使用ruby生成zip文件
如果已经拿到了csv文件,就用OO转成Excel成么? ---非程序员思路
-- by lgn21st






评论排行榜