设置“视图名称”一个控制器的

发布于 2024-10-25 05:59:22 字数 936 浏览 4 评论 0原文

我想集中一些控制器的类似操作,并编写一个其他控制器继承的控制器。这很好用。

# calling Configurations#index will render configurations/index.html.erb
# while 'configurations' being the internal controller_path used to look for the view
class ConfigurationsController < EditorController
end

class EditorController < ApplicationController
 def index
  render 'index'
 end
end

但现在我想将视图集中到“基本”控制器的视图,因此如果调用继承控制器,则使用的controller_path应该是基本控制器的。

有没有办法重写控制器名称或控制器路径?

我查看了 AbstractController::Base 的源代码,发现(第 90 行)

def controller_path
  @controller_path ||= name.sub(/Controller$/, '').underscore unless anonymous?
end

所以我只需要从我的基本控制器设置 @controller_path 不是吗?这不会改变任何东西:

#just does the same as above
class EditorController < ApplicationController
 @controller_path = 'editor'
 def index
  render 'index'
 end
end

那么有没有办法手动设置controller_path?

非常感谢!

I would like to centralize similar actions of some controllers and wrote a controller from which the other controllers inherites. This works fine.

# calling Configurations#index will render configurations/index.html.erb
# while 'configurations' being the internal controller_path used to look for the view
class ConfigurationsController < EditorController
end

class EditorController < ApplicationController
 def index
  render 'index'
 end
end

But now I would like to centralise the views to the "base"-controller one's, so if an inheriting controller is called, the controller_path used should be the base-controller one's.

Is there a way, to rewrite a controllers name or controller_path?

I looked at the source of AbstractController::Base and found that (line 90)

def controller_path
  @controller_path ||= name.sub(/Controller$/, '').underscore unless anonymous?
end

So I just need to set @controller_path from my base-controller don't I ? This doesn't change anything:

#just does the same as above
class EditorController < ApplicationController
 @controller_path = 'editor'
 def index
  render 'index'
 end
end

So is there a way to set the controller_path manually?

great thanks in advance!

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

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

发布评论

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

评论(1

究竟谁懂我的在乎 2024-11-01 05:59:22

该死的是我自己发现的!

我刚刚覆盖了controller_path方法:

class EditorController < ApplicationController
 def controller_path
  'editor'
 end
 #...
end

这将永远使用任何继承控制器的视图文件夹“编辑器”。

damn I found it on my own!

I just overwrote the controller_path method:

class EditorController < ApplicationController
 def controller_path
  'editor'
 end
 #...
end

this will ever use the view-folder 'editor' for any inheriting controller.

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