Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 + append_view_path

I want to use dynamic path in my controller in rails gem.

I've added to

module MyGem   class FooController < Config.controller     before_action ->{ append_view_path "app/views/my_gem/#{wizard_name}" } ... 

and in views I need to specify path like

app/views/my_gem/#{wizard_name}/my_gem/foo/some.erb 

is in Rails some way, to cut the relative path of gem namespace, and get lookup path like?

app/views/my_gem/#{wizard_name}/some.erb # or foo/bar/some.erb 

Thank you!

UPD:

I understand, that there is way with disabling isolate_namespace in Engine, but I believe, that it is not best and only option.

UPD2: For Rails4 this idea was very useful https://coderwall.com/p/p_yelg/render-view-outside-of-a-controller-in-rails-4

like image 203
Mike Belyakov Avatar asked Oct 31 '16 04:10

Mike Belyakov


1 Answers

The Devise gem has a way of adding views lookup path by overriding the _prefixes method:

class DeviseController < Devise.parent_controller.constantize   include Devise::Controllers::ScopedViews    # Override prefixes to consider the scoped view.   # Notice we need to check for the request due to a bug in   # Action Controller tests that forces _prefixes to be   # loaded before even having a request object.   #   # This method should be public as it is is in ActionPack   # itself. Changing its visibility may break other gems.   def _prefixes #:nodoc:     @_prefixes ||= if self.class.scoped_views? && request && devise_mapping       ["#{devise_mapping.scoped_path}/#{controller_name}"] + super     else       super     end   end  end 

Can this be applied to your use case?

See source:
https://github.com/plataformatec/devise/blob/master/app/controllers/devise_controller.rb

like image 125
pelarejo Avatar answered Sep 27 '22 19:09

pelarejo