Ruby-on-rails 如何更改 Rails 3 控制器中视图文件的默认路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4301249/
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:43:23  来源:igfitidea点击:

How to change the default path of view files in a Rails 3 controller?

ruby-on-railsrubymodel-view-controllerruby-on-rails-3

提问by Yuval Karmi

I have a controller called ProjectsController. Its actions, by default, look for views inside app/views/projects. I'd like to change that path for all methods (index, show, new, editetc...) in the controller.

我有一个名为ProjectsController. 默认情况下,它的操作会在app/views/projects. 我想改变这种路径的所有方法(indexshownewedit等...)在控制器中。

For example:

例如:

class ProjectsController < ApplicationController

  #I'd like to be able to do something like this
  views_path 'views/mycustomfolder'

  def index
    #some code
  end

  def show
    #some code
  end

  def new
    #some code
  end

  def edit
    #some code
  end
end

Please note I am not changing each method with renderbut defining a default path for all of them. Is this possible? If so, how?

请注意,我没有更改每个方法,render而是为所有方法定义默认路径。这可能吗?如果是这样,如何?

Thank you!

谢谢!

采纳答案by August Lilleaas

If there's no built-in method for this, perhaps you can override renderfor that controller?

如果没有为此的内置方法,也许您可​​以覆盖render该控制器?

class MyController < ApplicationController
  # actions ..

  private

  def render(*args)
    options = args.extract_options!
    options[:template] = "/mycustomfolder/#{params[:action]}"
    super(*(args << options))
  end
end

Not sure how well this works out in practice, or if it works at all.

不确定这在实践中的效果如何,或者它是否有效。

回答by mmell

See ActionView::ViewPaths::ClassMethods#prepend_view_path.

请参阅ActionView::ViewPaths::ClassMethods#prepend_view_path

class ProjectsController < ApplicationController
    prepend_view_path 'app/views/mycustomfolder'
    ...

回答by Ravenstine

You can do this inside your controller:

您可以在控制器中执行此操作:

  def self.controller_path
    "mycustomfolder"
  end

回答by Mike Gorski

You can add something like:

您可以添加如下内容:

paths.app.views << "app/views/myspecialdir"

in the config/application.rb file to have rails look in another directory for view templates. The one caveat is that it'll still look for view files that match the controller. So if you have a controller named HomeController with the above config for the views it'll look for something named "app/views/myspecialdir/home/index.html.erb" to render.

在 config/application.rb 文件中,让 rails 在另一个目录中查找视图模板。一个警告是它仍然会寻找与控制器匹配的视图文件。因此,如果您有一个名为 HomeController 且具有上述视图配置的控制器,它将寻找名为“app/views/myspecialdir/home/index.html.erb”的内容来呈现。

回答by Swaps

If you want to change the default path for all your views at app level, you could do something like following -

如果您想在应用程序级别更改所有视图的默认路径,您可以执行以下操作 -

class ApplicationController < ActionController::Base
  before_action :set_views

  private

  def set_views
    prepend_view_path "#{Rails.root.join('app', 'views', 'new_views')}"
  end
end

And write all your views in the folder new_viewsfollowing the same directory structure as original.

new_views按照与原始目录结构相同的目录结构在文件夹中写入所有视图。

P.S. - This answer is inspired from @mmell's answer.

PS - 这个答案的灵感来自@mmell的答案。