指定“app”路径的正确方法是什么?在 Rails 插件中?
出现这个问题是因为 cells gem 使用 File.join('app','cells') 指定模板目录。除非您将 Rails 作为守护进程运行(scripts/server -d),否则效果很好。守护进程将目录切换到 / ,这使得单元模板路径指向错误的绝对路径。
我的解决方案是将默认路径设置为 File.join(RAILS_ROOT, 'app', 'cells')。这在 Rails 中有效,但插件的单元测试失败,因为未定义 RAILS_ROOT。使用 File.join(File.dirname(__FILE__),'..'
... 也可以,但需要大约 6 个级别的 '..' 这似乎是错误的。
所以我的问题是什么是正确的在 Rails 插件中指定“app”下目录的路径的方法?还是有其他错误导致守护进程 Rails 无法找到相对路径?
This question came about because the cells gem specifies template directories using File.join('app','cells'). That works fine until you run Rails as a daemon (scripts/server -d). The daemon switches directories to / which leaves the cells template paths pointing to the wrong absolute path.
My solution was to set the default paths to File.join(RAILS_ROOT, 'app', 'cells'). This works in Rails, but the unit tests for the plugin fail because RAILS_ROOT isn't defined. Using File.join(File.dirname(__FILE__),'..'
... also works but requires about 6 levels of '..' which seems wrong.
So my question is what is the proper way to specify the path to a directory under 'app' in a Rails plugin? Or is there something else wrong that would cause daemonizing Rails to fail to find the relative paths?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议将您的更改从插件中移出并放入初始值设定项中。在初始化程序中重写使用 File.join('app','cells') 的方法。这有几个好处。
I suggest moving your changes out of the plugin and into an initializer. In the initializer override the method that uses File.join('app','cells'). This has several benefits.
对于那些难以破译这个答案的人,这对我有用:
创建一个文件 config/initializers/cells.rb
在其中
:希望这会有所帮助
For those who had trouble deciphering this answer, here is what worked for me:
Create a file config/initializers/cells.rb
in it put:
Hope this helps