【问题标题】:setting the "view-name" of a controller设置控制器的“视图名称”
【发布时间】:2011-03-24 00:48:19
【问题描述】:

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

# 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

但现在我想将视图集中到“基”控制器的视图,所以如果调用继承控制器,则使用的控制器路径应该是基控制器的。

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

我查看了 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?

非常感谢!

【问题讨论】:

    标签: ruby-on-rails-3 controller actionpack


    【解决方案1】:

    该死的我自己找到的!

    我只是重写了 controller_path 方法:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      相关资源
      最近更新 更多