How do I change the path to the default view file in a Rails 3 controller?

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

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 

Note that I am not modifying each method using render , but defining a default path for all of them. Is it possible? If so, how?

Thank!

+43
ruby ruby-on-rails model-view-controller ruby-on-rails-3
Nov 29 '10 at 6:08
source share
4 answers

If there is no built-in method for this, perhaps you can override render for this controller?

 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 in practice, or if it works at all.

+19
Nov 29 '10 at 6:12
source share

See ActionView :: ViewPaths :: ClassMethods # prepend_view_path .

 class ProjectsController < ApplicationController prepend_view_path 'app/views/mycustomfolder' ... 
+45
Oct 09
source share

You can do this inside your controller:

  def self.controller_path "mycustomfolder" end 
+26
Aug 08 '14 at 1:18
source share

You can add something like:

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

in the config / application.rb file to have rails in another directory for viewing templates. One caveat is that it will still look for view files matching the controller. Therefore, if you have a controller named HomeController with the above configuration for views, it will look for something called "app / views / myspecialdir / home / index.html.erb" for rendering.

+11
Nov 29 '10 at 15:48
source share



All Articles