【发布时间】:2011-03-30 19:02:34
【问题描述】:
我需要在我的插件模块中获取 url 信息。
request.request_uri 不可用。
是否有 ruby/rails 类似于 $_SERVER['REQUEST_URI'] 的 php?
例如:
module MyPlugin
module Routing
def self.getOpts
# HIRE I NEED TO ANALYZE URL and return hash with resulting param
return {controller: :divisions, action: :show, id: 11, as: :current}
end
end
end
# extend routing
module ActionDispatch::Routing
class Mapper
def my_rout
match 'articles', MyPlugin::Routing.getOpts
end
end
end
# In config/routes.rb
Rails::application.routes.draw do
my_rout
end
这就是我所需要的,例如:
- 我们得到一个网址http://mysite.ru/slug_division_1/slug_division_2
- ID 为 2 的部门在 DB 表中有一个字段“handler”,值为“any_controller#any_action”
- 在 MyPlugin::Routing 中,我正在分析 url 路径并从 DB 中获取值 'any_controller#any_action'
- MyPlugin::Routing.getOpts 返回参数 {controller::any_controller, action::any_action, id: 2, as::current}
-
从 ActionDispatch::Routing.Mapper.my_rout 我们像这样设置新的 rout
匹配'slug_division_1/slug_division_2',{控制器::any_controller,动作::any_action,id:2,as::当前}
只是一个小技巧。
【问题讨论】:
-
说什么?您想要
ruby或php中的答案吗? -
我试图通过比较 ruby 和 php 来理解主题
-
你能解释一下你想用显示的代码实现什么吗?就像@daemin 我觉得你做错了。
标签: ruby-on-rails url routing request