如何将操作路由到 Rails 3 中的应用程序控制器?

发布于 2024-10-14 12:11:02 字数 385 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

牵你手 2024-10-21 12:11:02

老帖子,但接受的答案是错误的。尽管它的精神是正确的 - 您可能应该将您的方法移至更合适的控制器;但如果您坚持在application_controller.rb中使用该方法

# routes.rb
match '/some_route', to: 'application#some_route', as: :some_route

...将路由到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

# routes.rb
match '/some_route', to: 'application#some_route', as: :some_route

...will route to the 'some_route' method in application_controller.rb.

[浮城] 2024-10-21 12:11:02

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 your text_field

遥远的她 2024-10-21 12:11:02

另一个死灵,但有一种更干燥友好的方法来做到这一点,与 pavling 的解决方案相比,

get :autocomplete_category_name, controller:"application"

我试图(昨天)将其移动到应用程序控制器,以尝试更好地重用代码,而不是将其绑定到特定控制器。直到今天我才意识到这是没有意义的,因为用于应答此调用的控制器将是与渲染视图的控制器完全不同的对象。

Another necro but there is a more DRY friendly way to do this, compared to pavling's solution

get :autocomplete_category_name, controller:"application"

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..

天荒地未老 2024-10-21 12:11:02

使用 Rails 4,你必须使用 get,而不是 match...

# routes.rb
get '/autocomplete_category_name', to: 'application#autocomplete_category_name', as: :autocomplete_category_name

Using Rails 4, your have to use get, not match...

# routes.rb
get '/autocomplete_category_name', to: 'application#autocomplete_category_name', as: :autocomplete_category_name
笑饮青盏花 2024-10-21 12:11:02

尝试类似的东西

match "home/autocomplete_category_name", "home#autocomplete_category_name", :as => "autocomplete_category_name"

try something like

match "home/autocomplete_category_name", "home#autocomplete_category_name", :as => "autocomplete_category_name"
靑春怀旧 2024-10-21 12:11:02

在 Rails 4 中,使用这个:

get 'application/autocomplete_category_name', as: 'autocomplete_category_name'

你有 'autocomplete_category_name_path' 链接

In Rails 4, use this:

get 'application/autocomplete_category_name', as: 'autocomplete_category_name'

You have 'autocomplete_category_name_path' to you links

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文