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
, 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
Please note I am not changing each method with render
but defining a default path for all of them. Is this possible? If so, how?
Thank you!
If you want to change the default path for all your views at app level, you could do something like following -
And write all your views in the folder
new_views
following the same directory structure as original.You can add something like:
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.
If there's no built-in method for this, perhaps you can override
render
for that controller?Not sure how well this works out in practice, or if it works at all.
You can do this inside your controller:
See ActionView::ViewPaths::ClassMethods#prepend_view_path.