如何将操作路由到 Rails 3 中的应用程序控制器?
我正在使用 gem Rails3-jquery-autocomplete 并且没有任何问题,但是我现在已将自动完成表单移至应用程序模板中,因此 ajax 调用现在由应用程序控制器处理,因此我的路线已更改为:
home/autocomplete_category_name
现在需要拆除房屋以及从:
home_autocomplete_category_name_path
到:
autocomplete_category_name_path
的路径有人有什么想法吗?仍在学习 Rails 的细节,所以现在这对我来说是一个死胡同。
谢谢。
I am using the gem rails3-jquery-autocomplete and had no problems with it, however I have now moved my autocomplete form into the application template and therefore the ajax calls are now being dealt by the application controller, so my routes have changed from:
home/autocomplete_category_name
and now need to have the home removed and the path from:
home_autocomplete_category_name_path
to:
autocomplete_category_name_path
Anybody got any ideas? Still learning the ins and outs of Rails so this is a dead end for me right now.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
老帖子,但接受的答案是错误的。尽管它的精神是正确的 - 您可能应该将您的方法移至更合适的控制器;但如果您坚持在application_controller.rb中使用该方法
...将路由到application_controller.rb中的“some_route”方法。
Old post, but the accepted answer is wrong. Though the spirit of it is right - you should probably move your method to a more appropriate controller; but if you insist on having the method in application_controller.rb
...will route to the 'some_route' method in application_controller.rb.
URL 不会直接映射到
ApplicationController
- 只是它的子类。您需要将对
autocomplete
的调用移至另一个控制器中。只要您在定义text_field
时传递正确的路径,表单的位置就不会产生影响URLs don't map directly to
ApplicationController
- only subclasses of it.You need to move the call to
autocomplete
into another controller. The location of the form shouldn't make a difference, as long as you're passing the correct path when you define yourtext_field
另一个死灵,但有一种更干燥友好的方法来做到这一点,与 pavling 的解决方案相比,
我试图(昨天)将其移动到应用程序控制器,以尝试更好地重用代码,而不是将其绑定到特定控制器。直到今天我才意识到这是没有意义的,因为用于应答此调用的控制器将是与渲染视图的控制器完全不同的对象。
Another necro but there is a more DRY friendly way to do this, compared to pavling's solution
I was trying(yesterday) to move it to the Application controller to try to reuse code better and not tie it to a specific controller. Only today did I realize that makes no sense, since the controller used to answer this call is going to be a completely different object to the controller rendering the view anyway..
使用 Rails 4,你必须使用 get,而不是 match...
Using Rails 4, your have to use get, not match...
尝试类似的东西
try something like
在 Rails 4 中,使用这个:
你有 'autocomplete_category_name_path' 链接
In Rails 4, use this:
You have 'autocomplete_category_name_path' to you links