Rails 4:你不应该在没有指定 HTTP 方法的情况下在路由器中使用 `match` 方法 [英] Rails 4: You should not use the `match` method in your router without specifying an HTTP method

查看:29
本文介绍了Rails 4:你不应该在没有指定 HTTP 方法的情况下在路由器中使用 `match` 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我已经升级到 Rails 4(在我的 10.9 服务器更新中有点计划外)并且能够在我的照片库应用程序上运行除路线之外的所有内容.出于某种原因,自从 rails 3 以来,我一直无法理解路线.这是我之前在 Rails 3 下的工作代码

Okay, so I've upgraded to Rails 4 (kind of unplanned with my 10.9 server update) and have been able to get everything running on my photo gallery app except for the routes. For some reason I've always had trouble understanding routes since rails 3. Here was my previous working code under Rails 3

root :to => "gallery#index", :as => "gallery"

get 'gallery' => 'gallery#index'
resources :galleries

match 'gallery_:id' => 'gallery#show', :as => 'gallery'

我知道 match 已折旧,但是如果我尝试使用 GET,我会收到以下错误:

I understand that match has been depreciated, but if I try to use GET, I'm getting the following error:

无效的路由名称,已在使用中:'gallery' 您可能已经使用 :as 选项定义了两条同名的路由,或者您可能正在覆盖已由资源定义的路由同名.

Invalid route name, already in use: 'gallery' You may have defined two routes with the same name using the :as option, or you may be overriding a route already defined by a resource with the same naming.

基本上,我希望根(索引)像它一样加载为/photos/gallery",并且我的显示动作要加载,例如,将 id 435 记录为:/photos/gallery_435",这就是我的方式以前有它的工作.抱歉,这可能是一个简单的问题,我似乎无法掌握 Rails 路由.

Basically, I want the root (index) to load as "/photos/gallery" as it does, and my show action to load, for example, record id 435 as: "/photos/gallery_435" which is how I previously had it working. Sorry for what is probably a simple question, I just cannot seem to grasp the rails routing.

推荐答案

试试这个

match 'gallery_:id' => 'gallery#show', :via => [:get], :as => 'gallery_show'

然后,您可以在帮助程序和视图中将此路径称为 gallery_show_path.

You can then refer to this path as gallery_show_path in your helpers and views.

更改as"会消除冲突.

Changing the 'as' removes the conflict.

这篇关于Rails 4:你不应该在没有指定 HTTP 方法的情况下在路由器中使用 `match` 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆