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

2.5k Views Asked by At

I have the following code in my view (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)

But...

order_submissions_path(order_submission.id) 

Creates a URL that comes out as:

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:

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'
3

There are 3 best solutions below

2
On BEST ANSWER

The order_submissions_path (plural) points to /order_submissions. It takes two arguments, the first being the format for the request (e.g. html). Your ID is being passed in for this argument, leading to the resulting URL you're seeing.

You actually want the singular path helper, order_submission_path, which accepts an ID as the first argument.

2
On

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. You can also remove id from the end.

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

3
On

You don't get a named route by default. The route you showed from rake routes doesn't list a named route, for example.

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

Normally, you'd see the named route in front of GET there.

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. Now, order_submission_path(order_submission.id) will work. (Note: .id is superfluous if your order_submission responds to to_path and returns 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. Perhaps your index route?