Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
like image 422
fuzzybabybunny Avatar asked Apr 01 '14 16:04

fuzzybabybunny


2 Answers

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.

like image 103
coreyward Avatar answered Oct 23 '22 12:10

coreyward


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

like image 27
BroiSatse Avatar answered Oct 23 '22 11:10

BroiSatse