Ruby-on-rails Rails 3:获取当前命名空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4226573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:40:03  来源:igfitidea点击:

Rails 3: Get current namespace?

ruby-on-railsruby-on-rails-3namespacesactioncontroller

提问by arnekolja

using a method :layout_for_namespace I set my app's layout depending on whether I am in frontend or backend, as the backend is using an namespace "admin".

使用方法 :layout_for_namespace 我根据我是在前端还是后端来设置应用程序的布局,因为后端使用命名空间“admin”。

I could not find a pretty way to find out which namespace I am, the only way I found is by parsing the string from params[:controller]. Of course that's easy, seems to be fail-safe and working good. But I am just wondering if there's a better, prepared, way to do this. Does anyone know?

我找不到一种很好的方法来找出我是哪个命名空间,我找到的唯一方法是解析来自 params[:controller] 的字符串。当然,这很容易,似乎是故障安全的并且运行良好。但我只是想知道是否有更好的、准备好的方法来做到这一点。有人知道吗?

Currently I am just using the following method:

目前我只是使用以下方法:

def is_backend_namespace?
  params[:controller].index("admin/") == 0
end

Thanks in advance

提前致谢

Arne

阿恩

回答by William Wong Garay

You can use:

您可以使用:

self.class.parent == Admin

回答by Ryan Crispin Heneise

Outside the controller (e.g. in the views), use controller.class.name. You can turn this into a helper method like this:

在控制器之外(例如在视图中),使用 controller.class.name。你可以把它变成这样的辅助方法:

module ApplicationHelper
  def admin?
    controller.class.name.split("::").first=="Admin"
  end
end

回答by KenB

In both the controller and the views, you can parse controller_path, eg.:

在控制器和视图中,您都可以解析 controller_path,例如:

namespace = controller_path.split('/').first

回答by johnmcaliley

Not much more elegant, but it uses the class instead of the params hash. I am not aware of a "prepared" way to do this without some parsing.

没有更优雅,但它使用类而不是 params 哈希。我不知道没有一些解析的“准备”方式来做到这一点。

self.class.to_s.split("::").first=="Admin"

回答by Jason Harrelson

None of these solutions consider a constant with multiple parent modules. For instance:

这些解决方案都没有考虑具有多个父模块的常量。例如:

A::B::C

As of Rails 3.2.x you can simply:

从 Rails 3.2.x 开始,您可以简单地:

"A::B::C".deconstantize #=> "A::B"

As of Rails 3.1.x you can:

从 Rails 3.1.x 开始,您可以:

constant_name = "A::B::C"
constant_name.gsub( "::#{constant_name.demodulize}", '' )

This is because #demodulize is the opposite of #deconstantize:

这是因为#demodulize 与#deconstantize 相反:

"A::B::C".demodulize #=> "C"

If you really need to do this manually, try this:

如果您确实需要手动执行此操作,请尝试以下操作:

constant_name = "A::B::C"
constant_name.split( '::' )[0,constant_name.split( '::' ).length-1]

回答by Danny

Setting the namespace in application controller:

在应用程序控制器中设置命名空间:

path = self.controller_path.split('/')
@namespace = path.second ? path.first : nil

回答by Sami Birnbaum

Rails 6

导轨 6

Accessing the namespace in the view?

访问视图中的命名空间?

do not use: controller.namespace.parent == Admin

不使用: controller.namespace.parent == Admin

the parentmethod will be removed in Rails 6.1

parent方法将在 Rails 6.1 中删除

DEPRECATION WARNING: `Module#parent` has been renamed to `module_parent`. `parent` is deprecated and will be removed in Rails 6.1.

use module_parentinstead:

使用module_parent来代替:

controller.namespace.module_parent == Admin

controller.namespace.module_parent == Admin

回答by staxim

In Rails 6, the controller class does not seem to have a namespacemethod on it.

Rails 6 中,控制器类似乎没有namespace方法。

The solution that seemed cleanest and worked for me in a view was: controller.class.module_parent

看起来最干净并且对我有用的解决方案是: controller.class.module_parent

Specifically, if your namespace is Admin:: and you wanted 'admin', you'd do: controller.class.module_parent.to_s.downcase

具体来说,如果你的命名空间是 Admin:: 并且你想要“admin”,你会这样做: controller.class.module_parent.to_s.downcase