简体   繁体   English

为什么Ruby on Rails的URL Helper在我的URL中放了一段时间?

[英]Why did Ruby on Rails' URL Helper put a period in my URL?

I have the following code in my view (RoR 4): 我在视图中有以下代码(RoR 4):

tbody
  - @order_submissions.each do |order_submission|
    tr
      td = order_submission.id
      td.table-actions
        span = link_to "Show", order_submissions_path(order_submission.id)

td = order_submission.id 

successfully displays as the ID number (533ab7337764690d6d000000) 成功显示为ID号(533ab7337764690d6d000000)

But... 但...

order_submissions_path(order_submission.id) 

Creates a URL that comes out as: 创建一个以下列形式出现的URL:

order_submissions.533ab7337764690d6d000000

I want it to be 我想要它

order_submissions/533ab7337764690d6d000000

Where did that period come from? 那个时期从哪里来?

This is my route: 这是我的路线:

get 'order_submissions/:id'         => 'order_submissions#show'

And when I run rake routes I get: 当我运行rake路线时,我得到:

GET    /order_submissions/:id(.:format)        order_submissions#show

The (.:format) is probably what's messing it up but I don't know why. (。:格式)可能正在弄乱它,但我不知道为什么。 I just want it to put a slash in there. 我只是想让它在那里放一个斜线。

If I change my code to this it fixes it: 如果我将我的代码更改为此修复它:

 span = link_to "Show", order_submissions_path + '/' + order_submission.id

But that's a really, really stupid workaround. 但这是一个非常非常愚蠢的解决方法。

EDIT: Here are my routes: 编辑:这是我的路线:

   get 'order_submissions'             => 'order_submissions#index'
   get 'order_submissions/new'         => 'order_submissions#new'
   post 'order_submissions'            => 'order_submissions#create'
   get 'order_submissions/:id'         => 'order_submissions#show'
   get 'order_submissions/:id/edit'    => 'order_submissions#edit'
   patch 'order_submissions/:id'       => 'order_submissions#update'
   get 'order_submissions/:id/delete'  => 'order_submissions#delete'
   delete 'order_submissions/:id'      => 'order_submissions#destroy'

The order_submissions_path (plural) points to /order_submissions . order_submissions_path (复数)指向/order_submissions It takes two arguments, the first being the format for the request (eg html). 它需要两个参数,第一个是请求的格式(例如html)。 Your ID is being passed in for this argument, leading to the resulting URL you're seeing. 您的ID将被传入此参数,从而导致您看到的结果URL。

You actually want the singular path helper, order_submission_path , which accepts an ID as the first argument. 您实际上需要单个路径助手order_submission_path ,它接受一个I​​D作为第一个参数。

Because it should be a singular form: 因为它应该是一个单数形式:

order_submission_path(order_submission.id) 

Not

order_submissions_path(order_submission.id)

order_submissions_path points onto index action. order_submissions_path指向索引操作。 You can also remove id from the end. 您也可以从最后删除id

UPDATE: 更新:

Just notice you route file. 只需注意你的路线文件。 Do you have any resources defined there? 你有资源吗? The route you posted wouldn't generate any url_helper as you dind't specify route name (most likely this entry is obsolete, as I expect there is resources :order_submissions somewhere there as well). 您发布的路由不会生成任何url_helper,因为您没有指定路由名称(很可能此条目已过时,因为我预计有resources :order_submissions那里)。

You don't get a named route by default. 默认情况下,您没有获得命名路由。 The route you showed from rake routes doesn't list a named route, for example. 例如,您从rake routes显示的rake routes未列出命名路线。

GET /order_submissions/:id(.:format) order_submissions#show

Normally, you'd see the named route in front of GET there. 通常情况下,你会在那里看到GET前面的指定路线。

So you can define it yourself and then your route will work: 所以你可以自己定义它,然后你的路线就可以了:

get 'order_submissions/:id' => 'order_submissions#show', as: :order_submission

Notice the as: :order_submission bit. 注意as: :order_submission位。 Now, order_submission_path(order_submission.id) will work. 现在, order_submission_path(order_submission.id)将起作用。 (Note: .id is superfluous if your order_submission responds to to_path and returns id .) (注意:如果你的order_submission响应to_path并返回id .id是多余的。)

I'm guessing you have another route in your rake routes output that uses the named route you supplied and that doesn't use /:id . 我猜你在你的rake routes输出中有另一条路由,它使用你提供的命名路由并且不使用/:id Perhaps your index route? 也许你的指数路线?

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

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