Ruby-on-rails 安装导轨引擎中的命名路线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8029192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Named routes in mounted rails engine
提问by Markus
I'm making a small rails engine which I mount like this:
我正在制作一个小型导轨引擎,我像这样安装:
mount BasicApp::Engine => "/app"
Using this answerI have verified that all the routes in the engine are as the should be:
使用这个答案,我已经验证引擎中的所有路由都应该是:
However - when I (inside the engine) link to a named route (defined inside the engine) I get this error
但是 - 当我(在引擎内)链接到命名路由(在引擎内定义)时,我收到此错误
undefined local variable or method `new_post_path' for #<#<Class:0x000000065e0c08>:0x000000065d71d0>
Running "rake route" clearly verifies that "new_post" should be a named path, so I have no idea why Rails (3.1.0) can't figure it out. Any help is welcome
运行“rake route”清楚地验证“new_post”应该是一个命名路径,所以我不知道为什么Rails(3.1.0)无法弄清楚。欢迎任何帮助
my config/route.rb (for the engine) look like this
我的 config/route.rb(用于引擎)看起来像这样
BasicApp::Engine.routes.draw do
resources :posts, :path => '' do
resources :post_comments
resources :post_images
end
end
I should add that it is and isolated engine. However paths like main_app.root_path works fine - while root_path does not
我应该补充一点,它是独立的引擎。然而,像 main_app.root_path 这样的路径工作正常 - 而 root_path 没有
回答by James A. Rosen
The right way
正确的方式
I believe the best solution is to call new_post_pathon the Engine's routes proxy, which is available as a helper method. In your case, the helper method will default to basic_app_engine, so you can call basic_app_engine.new_post_pathin your views or helpers.
我相信最好的解决方案是调用new_post_path引擎的路由代理,它可以作为辅助方法使用。在您的情况下,助手方法将默认为basic_app_engine,因此您可以调用basic_app_engine.new_post_path您的视图或助手。
If you want, you can set the name in one of two ways.
如果需要,您可以通过以下两种方式之一设置名称。
# in engine/lib/basic_app/engine.rb:
module BasicApp
class Engine < ::Rails::Engine
engine_name 'basic'
end
end
or
或者
# in app/config/routes.rb:
mount BasicApp::Engine => '/app', :as => 'basic'
In either case, you could then call basic.new_posts_pathin your views or helpers.
无论哪种情况,您都可以调用basic.new_posts_path您的视图或助手。
Another way
其它的办法
Another option is to not use a mounted engine and instead have the engine add the routes directly to the app. Thoughtbot's HighVoltagedoes this. I don't love this solution because it is likely to cause namespace conflicts when you add many engines, but it does work.
另一种选择是不使用安装的引擎,而是让引擎直接将路由添加到应用程序。Thoughtbot 的 HighVoltage 可以做到这一点。我不喜欢这个解决方案,因为当你添加很多引擎时它很可能会导致命名空间冲突,但它确实有效。
# in engine/config/routes.rb
Rails.application.routes.draw do
resources :posts, :path => '' do
resources :post_comments
resources :post_images
end
end
# in app/config/routes.rb:
# (no mention of the engine)
回答by Epigene
On Rails 4 the engine_namedirective did not work for me.
To access a named route defined in engine's routes from engine's own view or controller, I ended up using the verbose
在 Rails 4 上,该engine_name指令对我不起作用。
为了从引擎自己的视图或控制器访问引擎路由中定义的命名路由,我最终使用了verbose
BasicApp::Engine.routes.url_helpers.new_post_path
I recommend defining a simple helper method to make this more usable
我建议定义一个简单的辅助方法以使其更有用
# in /helpers/basic_app/application_helper.rb
module BasicApp::ApplicationHelper
def basic_app_engine
@@basic_app_engine_url_helpers ||= BasicApp::Engine.routes.url_helpers
end
end
With this in place you can now use
有了这个,你现在可以使用
basic_app_engine.new_post_path
In case you need to access your main application helper from the engine you can just use main_app:
如果您需要从引擎访问您的主要应用程序助手,您可以使用main_app:
main_app.root_path
回答by edrich13
use the below in you app to access the engine routes
在您的应用程序中使用以下内容访问引擎路线
MyApp::Engine.routes.url_helpers.new_post_path

