如何在我的插件模块中获取 Ruby/Rails 中的当前 URL?

发布于 2024-10-29 02:54:42 字数 1273 浏览 1 评论 0原文

我需要在我的插件模块中获取 url 信息。

request.request_uri 不可用。

ruby/rails 是否与 php 类似 $_SERVER['REQUEST_URI']

例如:

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

这就是我需要的例如:

  1. 我们得到一个网址 http://mysite.ru/slug_division_1/slug_division_2
  2. id 2 的分区在数据库表中有一个字段“handler”,其值为“any_controller#any_action”
  3. 在 MyPlugin::Routing 中,我正在分析 url 路径并从数据库获取值“any_controller#any_action”
  4. MyPlugin::Routing .getOpts return params {controller: :any_controller, action: :any_action, id: 2, as: :current}
  5. 从 ActionDispatch::Routing.Mapper.my_rout 我们像这样设置新路由

    match 'slug_division_1/slug_division_2', {controller: :any_controller, action: :any_action, id: 2, as: :current}

只是一点小技巧。

I need to get url info in my plugin's module.

request.request_uri is unavailable.

Has ruby/rails an analog of $_SERVER['REQUEST_URI'] as php?

For example:

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

That's what I need for example:

  1. We get an url http://mysite.ru/slug_division_1/slug_division_2
  2. division with id 2 have in DB table a field 'handler' with value 'any_controller#any_action'
  3. In MyPlugin::Routing i'm doing analyze the url path and get from DB the value 'any_controller#any_action'
  4. MyPlugin::Routing.getOpts return params {controller: :any_controller, action: :any_action, id: 2, as: :current}
  5. From ActionDispatch::Routing.Mapper.my_rout we set new rout like this

    match 'slug_division_1/slug_division_2', {controller: :any_controller, action: :any_action, id: 2, as: :current}

Just a little hack.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

惟欲睡 2024-11-05 02:54:42

如何使 request_uri 成为 Rails 中的全局变量?

1) 使用以下代码添加到文件夹“vendor/plugins/myplugin/lib/myplugin”文件 request_global.rb

module Rack
  class MethodOverrideWithParams < Rack::MethodOverride
    def call(env)
        $request = Rack::Request.new(env) # $request is global vriable
        super(env)
    end
  end
end

2) 在 vendor/plugins/myplugin/lib/myplugin.rb code> 添加:

require 'myplugin/request_global'

3) 在 config/application.rb 添加:

config.middleware.swap 'Rack::MethodOverride', 'Rack::MethodOverrideWithParams'

4) 瞧,现在可以从 Rails 应用程序的任何地方使用 $request

就我而言,这是没有用的,因为每次对每个 $request 进行数据库查询以获取处理程序都需要重新加载路由(Rails::Application.reload_routes!)。它会降低性能。定义所有可能的路线会更有利可图,即使这些路线有几千条。仅当有人编辑了分区时才会重新加载路线。

How to make the request_uri a global variable in rails?

1) Add to folder 'vendor/plugins/myplugin/lib/myplugin' file request_global.rb with folowing code:

module Rack
  class MethodOverrideWithParams < Rack::MethodOverride
    def call(env)
        $request = Rack::Request.new(env) # $request is global vriable
        super(env)
    end
  end
end

2) In vendor/plugins/myplugin/lib/myplugin.rb add:

require 'myplugin/request_global'

3) In config/application.rb add:

config.middleware.swap 'Rack::MethodOverride', 'Rack::MethodOverrideWithParams'

4) Voilà, $request is now available from anywhere point the Rails application!

In my case it's useless because doing every time a database query for each $request to get the handler need to reload the routes (Rails::Application.reload_routes!). It degrades the performance. Defining all possible routes is more profitable even if these routes a few thousand. Reload the routes occurs only if someone edited the divisions.

无语# 2024-11-05 02:54:42

是的,请尝试使用 request.request_uri

Yes, try using request.request_uri.

诗酒趁年少 2024-11-05 02:54:42

request.request_uri 是查找您想要的信息的方法。如果您无权访问它,那么您需要向函数添加一个参数,并从某个有权访问的地方调用它。

从内存控制器确实可以访问request_uri。


我认为您真正想要的只是表中接受任何路径的一个控制器/操作路由,并在控制器操作中进行查找并调用相关函数。检查路径通配(在 Rails 2.3 中使用“*”字符,不确定您是否使用 Rails 3),并查看这是否适合您正在做的事情。

然后,您将拥有一条采用很长路径的路由,控制器和操作将该路径分解为自己的部分,执行查找,并调用适当的函数(或其他控制器/操作)。

但我不得不说几句我认为你做错了的事情。

除非您正在做一些非常棘手且非常不寻常的事情,否则不要将控制器和操作存储在数据库中。这就是红宝石路线本身的意义。如果您需要将这些存储在数据库中,那么在我看来您做错了。

您不应该将 ID 号硬编码到路线中,而应将其设置为一个变量。因此,不要硬编码 /some/thing/23 来匹配 'some/thing/23',而是让路由匹配 'some/thing/:id',你会得到 :id => 23 自动。

request.request_uri is the way to find the information you want. If you don't have access to it then you need to add a parameter to your function and call it from some place that does have access.

From memory Controllers do have access to the request_uri.


I think what you actually want is just one controller/action route in your table that accepts any paths, and in the controller action do the lookup and call the relevant function. Check out path globbing (using the '*' character in Rails 2.3, not sure if you're using Rails 3), and see if that fits what you are doing.

Then what you'll have is a single route that takes a long path, the controller and action breaks that path up into its own parts, performs the lookup, and calls the appropriate function (or other controller/action).

However I have to say a few words about a few things that I think you're doing wrong.

Unless you're doing something very tricky and quite out of the ordinary, don't store the controller and action in the database. That's what the ruby routes are for themselves. If you need to store these in a database then it sounds to me like you're doing it wrong.

You shouldn't hard code an id number into a route, let that be a variable that gets set. So instead of hard coding /some/thing/23 to match 'some/thing/23', instead make the route match 'some/thing/:id', and you'll get :id => 23 automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文