我想在 Rails gem 的 Controller 中使用动态路径。
我已经添加到
module MyGem
class FooController < Config.controller
before_action ->{ append_view_path "app/views/my_gem/#{wizard_name}" }
...
在 View 中我需要指定像
这样的路径app/views/my_gem/#{wizard_name}/my_gem/foo/some.erb
在 Rails 中以某种方式切割 gem 命名空间的相对路径,并获得类似的查找路径?
app/views/my_gem/#{wizard_name}/some.erb
# or
foo/bar/some.erb
谢谢!
更新:
我知道,在 Engine
中禁用 isolate_namespace
是有办法的,但我相信,这不是最好的,也是唯一的选择。
UPD2:对于 Rails4,这个想法非常有用 https://coderwall.com/p/p_yelg/render-view-outside-of-a-controller-in-rails-4
最佳答案
Devise gem 有一种通过覆盖 _prefixes 方法来添加 View 查找路径的方法:
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
这可以应用于您的用例吗?
查看来源:
https://github.com/plataformatec/devise/blob/master/app/controllers/devise_controller.rb
关于ruby-on-rails - Rails 4 + append_view_path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40336777/