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

前端 未结 5 2132
甜味超标
甜味超标 2020-12-01 06:16

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

5条回答
  •  广开言路
    2020-12-01 06:43

    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_views following the same directory structure as original.

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

提交回复
热议问题