Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在RubyonRails中以编程方式找到名称空间/模块名?_Ruby On Rails_Ruby_Namespaces - Fatal编程技术网

Ruby on rails 如何在RubyonRails中以编程方式找到名称空间/模块名?

Ruby on rails 如何在RubyonRails中以编程方式找到名称空间/模块名?,ruby-on-rails,ruby,namespaces,Ruby On Rails,Ruby,Namespaces,如何在下面的筛选器中找到命名空间或模块“Foo”的名称 class ApplicationController < ActionController::Base def get_module_name @module_name = ??? end end class Foo::BarController < ApplicationController before_filter :get_module_name end class ApplicationCo

如何在下面的筛选器中找到命名空间或模块“Foo”的名称

class ApplicationController < ActionController::Base
  def get_module_name
    @module_name = ???
  end
end


class Foo::BarController < ApplicationController
  before_filter :get_module_name
end
class ApplicationController
这应该可以做到:

  def get_module_name
    @module_name = self.class.to_s.split("::").first
  end
我不认为有更干净的方法,我在别的地方也见过

class ApplicationController < ActionController::Base
  def get_module_name
    @module_name = self.class.name.split("::").first
  end
end
class ApplicationController
如果控制器确实有模块名,则此操作有效;如果控制器没有模块名,则返回控制器名

class ApplicationController < ActionController::Base
  def get_module_name
    @module_name = self.class.name.split("::").first
  end
end
class ApplicationController
但是,如果我们将其更改为:

class ApplicatioNController < ActionController::Base
  def get_module_name
    my_class_name = self.class.name
    if my_class_name.index("::").nil? then
      @module_name = nil
    else
      @module_name = my_class_name.split("::").first
    end
  end
end
class ApplicatioNController

您可以确定该类是否具有模块名,并返回除可测试的类名以外的其他内容。

我知道这是一个旧线程,但我刚刚发现需要根据控制器的名称空间进行单独导航。我提出的解决方案是在我的应用程序布局中:

<%= render "#{controller.class.name[/^(\w*)::\w*$/, 1].try(:downcase)}/nav" %>
或在管理命名空间中:

app/views/_nav.html.erb
app/views/admin/_nav.html.erb

当然,每个名称空间都必须存在这些部分,否则会发生错误。现在,每个命名空间的导航将出现在每个控制器上,而不必改变任何控制器或视图。

< P>这些解决方案中没有一个考虑多个父模块的常量。例如:

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}", '' )
这是因为#解调与#解构相反:

"A::B::C".demodulize #=> "C"
如果确实需要手动执行此操作,请尝试以下操作:

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

我建议使用
gsub
而不是
split
。如果您不需要任何其他模块名称,那么拆分
会更有效

class ApplicationController < ActionController::Base
  def get_module_name
    @module_name = self.class.to_s.gsub(/::.*/, '')
  end
end
class ApplicationController
my_class.name.下划线.split('/').slice(0..-2)


my_class.name.split(“::”).slice(0..-2)

对于简单的情况,您可以使用:

self.class.parent
有许多子模块:

module ApplicationHelper
  def namespace
    controller.class.name.gsub(/(::)?\w+Controller$/, '')
  end
end

示例:
Foo::Bar::BazController
=>
Foo::Bar
没有人提到使用
rpartition

const_name = 'A::B::C'
namespace, _sep, module_name = const_name.rpartition('::')
# or if you just need the namespace
namespace = const_name.rpartition('::').first
对于Rails 6.1
self.class.module\u家长


Hettomei answer在Rails 6.0之前运行良好

弃用警告:
Module#parent
已重命名为
Module#u parent
parent
已弃用,将在Rails 6.1中删除


或者你也可以使用self.class.name.demodulize。不,你不能,实际上demodulize返回的类名没有名称空间正如Jason Harrelson指出的那样,这并没有考虑到多个父模块。这个问题是在Rails 3之前写的,模块实际上不受支持。更新了已接受的答案,以反映更多最新版本!Demoulize是关键的答案,只有在最近的红宝石?真不敢相信有这么多关于“黑魔法”的答案,而且直到2015年才有一个引用这种方法的答案在文档中找不到。。。我甚至不能复制:)我现在更喜欢这个答案::C类;结束;C.new.class.superclass->object我认为我的解决方案之所以有效是因为rails:
模块\u父项自rails 6.0以来就一直存在