安装导轨引擎中的命名路线
我正在制作一个小型 Rails 引擎,如下安装:
mount BasicApp::Engine => "/app"
使用 这个答案 我已经验证了引擎中的所有路由都应该是这样:
但是 - 当我(在引擎内部)链接到命名路由(在引擎内部定义)时,我收到此错误
undefined local variable or method `new_post_path' for #<#<Class:0x000000065e0c08>:0x000000065d71d0>
正在运行“耙路”清晰验证“new_post”应该是一个命名路径,所以我不知道为什么Rails(3.1.0)无法弄清楚它。欢迎任何帮助,
我的 config/route.rb (对于引擎)看起来像这样,
BasicApp::Engine.routes.draw do
resources :posts, :path => '' do
resources :post_comments
resources :post_images
end
end
我应该补充一点,它是独立的引擎。然而像 main_app.root_path 这样的路径工作正常 - 而 root_path 则不行
I'm making a small rails engine which I mount like this:
mount BasicApp::Engine => "/app"
Using this answer I 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
my config/route.rb (for the engine) look like this
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为最好的解决方案的正确方法
是在引擎的路由代理上调用
new_post_path
,该代理可作为辅助方法使用。在您的情况下,帮助程序方法将默认为basic_app_engine
,因此您可以在视图或帮助程序中调用basic_app_engine.new_post_path
。如果需要,您可以通过两种方式之一设置名称。
或者
无论哪种情况,您都可以在视图或助手中调用
basic.new_posts_path
。另
一种选择是不使用已安装的引擎,而是让引擎将路由直接添加到应用程序。 Thoughtbot 的 HighVoltage 可以实现此目的。我不喜欢这个解决方案,因为当您添加许多引擎时它可能会导致命名空间冲突,但它确实有效。
The right way
I believe the best solution is to call
new_post_path
on the Engine's routes proxy, which is available as a helper method. In your case, the helper method will default tobasic_app_engine
, so you can callbasic_app_engine.new_post_path
in your views or helpers.If you want, you can set the name in one of two ways.
or
In either case, you could then call
basic.new_posts_path
in your views or helpers.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 HighVoltage does this. I don't love this solution because it is likely to cause namespace conflicts when you add many engines, but it does work.
在 Rails 4 上,
engine_name
指令对我不起作用。要从引擎自己的视图或控制器访问引擎路由中定义的命名路由,我最终使用了详细的方法,
我建议定义一个简单的辅助方法以使其更可用
有了这个,您现在可以使用
如果您需要访问您的主引擎中的应用程序助手,您只需使用
main_app
:On Rails 4 the
engine_name
directive 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
I recommend defining a simple helper method to make this more usable
With this in place you can now use
In case you need to access your main application helper from the engine you can just use
main_app
:在您的应用程序中使用以下内容来访问引擎路线
use the below in you app to access the engine routes