简体   繁体   English

Ruby on Rails路由通配符URL

[英]Ruby on Rails Routing for Wildcard URL

I was trying to pull a segment from the URL and since I'm a rookie, kind of confused by the routing. 我试图从URL中提取一个段,由于我是一个菜鸟,因此对路由感到困惑。

My code to get the last segment works fine, it's my routing that is messed up. 我的代码可以正常工作,这是我的路由搞砸了。

Ideally the URL would like this: 理想情况下,URL会这样:

http://localhost.com/track/item/1234

I'm using Rails v4.0.8 & Ruby v2.0.0p451 我正在使用Rails v4.0.8和Ruby v2.0.0p451

The error is No route matches [GET] "/track/item/1234" 错误是没有路线匹配[GET]“ / track / item / 1234”

Here's the whole routes.rb file: 这是整个routes.rb文件:

SepContact::Application.routes.draw do

  get "track/item"
  get "track/item/:id"  
  get "contacts/contact"
  resources "contacts", only: [:new, :create]

end

Your routes should be like: 您的路线应为:

SepContact::Application.routes.draw do

  get "track/item/:id", to: 'controller#action'
  get "track/item", to: 'controller#action' 
  get "contacts/contact" to: 'controller#action' 
  resources :contacts, only: [:new, :create]

end

You need to specify a to: pointing to a controller and action unless you use the resource or resources helper. 您需要指定to:指向控制器和操作,除非您使用resourceresources助手。

I think CWitty's should work as well but here is a more explicit format. 我认为CWitty也应该工作,但这是一种更明确的格式。

  match "track/items",      to: 'controller#index', via: :get, as: "items"
  match "track/items/:id",  to: 'controller#show', via: :get, as: "item"

Note I updated your url to be more rails like items rather than item 请注意,我更新了您的网址,使其更像是项而不是项

I think most of your problem is with the track segment of the url. 我认为您的大部分问题与网址的跟踪段有关。 I don't see how get 'track/items' would map the the items#index controller / method I think the match method would be needed here to explicitly map the url to the correct controller and method. 我看不到get'track / items'如何映射items#index控制器/方法,我认为这里需要match方法将网址显式映射到正确的控制器和方法。

Is there a good reason you are naming you url like that? 您是否有充分的理由这样称呼您的网址?

You can read all about routing here: http://guides.rubyonrails.org/routing.html 您可以在此处阅读有关路由的所有信息: http : //guides.rubyonrails.org/routing.html

Here is the section of the above document that discusses using the match method: 这是上述文档中讨论使用match方法的部分:

3.7 HTTP Verb Constraints In general, you should use the get, post, put, patch and delete methods to constrain a route to a particular verb. 3.7 HTTP动词约束通常,应该使用get,post,put,patch和delete方法来约束到特定动词的路由。 You can use the match method with the :via option to match multiple verbs at once: 您可以将match方法与:via选项一起使用,以一次匹配多个动词:

match 'photos', to: 'photos#show', via: [:get, :post]

You can match all verbs to a particular route using via: :all: 您可以使用以下命令将所有动词与特定路线匹配::all:

match 'photos', to: 'photos#show', via: :all

Routing both GET and POST requests to a single action has security implications. 将GET和POST请求都路由到单个操作会带来安全隐患。 In general, you should avoid routing all verbs to an action unless you have a good reason to. 通常,除非有充分的理由,否则应避免将所有动词路由到一个动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM