简体   繁体   English

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

[英]How to get current URL in Ruby/Rails in my plugin's module?

I need to get url info in my plugin's module. 我需要在我的插件模块中获取url信息。

request.request_uri is unavailable. request.request_uri不可用。

Has ruby/rails an analog of $_SERVER['REQUEST_URI'] as php? ruby / rails类似于$_SERVER['REQUEST_URI']作为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 我们得到一个网址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' id为2的除法在DB表中有一个字段'handler',其值为'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' 在MyPlugin :: Routing我正在分析url路径并从D​​B获取值'any_controller#any_action'
  4. MyPlugin::Routing.getOpts return params {controller: :any_controller, action: :any_action, id: 2, as: :current} MyPlugin :: Routing.getOpts返回params {controller :: any_controller,action :: any_action,id:2,as :: current}
  5. From ActionDispatch::Routing.Mapper.my_rout we set new rout like this 从ActionDispatch :: Routing.Mapper.my_rout我们设置这样的新溃败

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

Just a little hack. 只是一点点破解。

How to make the request_uri a global variable in rails? 如何使request_uri成为rails中的全局变量?

1) Add to folder 'vendor/plugins/myplugin/lib/myplugin' file request_global.rb with folowing code: 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) In vendor/plugins/myplugin/lib/myplugin.rb add: 2)在vendor/plugins/myplugin/lib/myplugin.rb添加:

require 'myplugin/request_global'

3) In config/application.rb add: 3)在config/application.rb添加:

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

4) Voilà, $request is now available from anywhere point the Rails application! 4)Voilà, $request现在可以从Rails应用程序的任何地方获得!

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! ). 在我的情况下,它是无用的,因为每次为每个$request数据库查询以获取处理程序需要重新加载路由( 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. 仅当有人编辑了部门时才重新加载路线。

是的,尝试使用request.request_uri

request.request_uri is the way to find the information you want. request.request_uri是查找所需信息的方法。 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. 从内存控制器可以访问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. 检查路径通配(在Rails 2.3中使用'*'字符,不确定你是否使用Rails 3),看看它是否适合你正在做的事情。

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. 您不应该将ID号硬编码到路由中,让它成为一个被设置的变量。 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. 因此,而不是硬编码/ some / thing / 23来匹配'some / thing / 23',而是使路线匹配'some / thing /:id',你会得到:id => 23自动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM