Rails 3:获取当前名称空间?

发布于 2024-10-03 13:21:05 字数 347 浏览 7 评论 0原文

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

我找不到一种很好的方法来找出我所在的命名空间,我找到的唯一方法是解析 params[:controller] 中的字符串。当然,这很容易,似乎是自动防故障的并且工作良好。但我只是想知道是否有更好的、准备好的方法来做到这一点。有谁知道吗?

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

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

提前感谢

Arne

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".

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?

Currently I am just using the following method:

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

Thanks in advance

Arne

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

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

发布评论

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

评论(9

智商已欠费 2024-10-10 13:21:05

您可以使用:

self.class.parent == Admin

You can use:

self.class.parent == Admin
水溶 2024-10-10 13:21:05

在控制器外部(例如在视图中),使用controller.class.name。您可以将其转换为辅助方法,如下所示:

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

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

module ApplicationHelper
  def admin?
    controller.class.name.split("::").first=="Admin"
  end
end
月下客 2024-10-10 13:21:05

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

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

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

namespace = controller_path.split('/').first
小红帽 2024-10-10 13:21:05

没有更优雅,但它使用类而不是参数哈希。我不知道有没有一种“准备好的”方法可以在不进行解析的情况下执行此操作。

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

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.

self.class.to_s.split("::").first=="Admin"
ぃ双果 2024-10-10 13:21:05

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

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

Setting the namespace in application controller:

path = self.controller_path.split('/')
@namespace = path.second ? path.first : nil
过度放纵 2024-10-10 13:21:05

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

A::B::C

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

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

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

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

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

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

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

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

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:

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

As of Rails 3.1.x you can:

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

This is because #demodulize is the opposite of #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]
夏の忆 2024-10-10 13:21:05

在Rails 6中,控制器类似乎没有命名空间方法。

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

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

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

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

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

娇俏 2024-10-10 13:21:05

Rails 6

访问视图中的命名空间?

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

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.

使用 module_parent 代替:

controller .namespace.module_parent == 管理

Rails 6

Accessing the namespace in the view?

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

the parent method will be removed in 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_parent instead:

controller.namespace.module_parent == Admin

不及他 2024-10-10 13:21:05

在 Rails 6 中你可以这样做:

controller.class.module_parent

获取父模块。还没有尝试过多重嵌套。但对于一个你可以转换为这样的符号:

controller.class.module_parent.to_s.underscore.to_sym

In Rails 6 you can do:

controller.class.module_parent

to get the parent module. Haven't tried it with multiple nestings. But for one you can convert to a symbol like this:

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