5

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'
fuzzybabybunny
  • 5,146
  • 6
  • 32
  • 58

3 Answers3

9

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.

coreyward
  • 77,547
  • 20
  • 137
  • 166
  • Ohhhhh... I see. Changing from the plural to the singular worked. So basically I've got two different paths: order_submissions_path -- this is for CRUD routes dealing with entire collections of objects order_submission_path -- this is for CRUD routes dealing with singular objects Does that sound right? BTW, what's the difference between order_submissions_path and order_submissions_url ? – fuzzybabybunny Apr 01 '14 at 17:13
  • @user2760030 There are a number of route helpers, and it's a little more nuanced than what you've stated here, but you're getting the idea. You should check out the [Rails Guide on Routing](http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions) for a more in-depth description. – coreyward Apr 01 '14 at 17:46
4

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

BroiSatse
  • 44,031
  • 8
  • 61
  • 86
2

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?

pdobb
  • 17,688
  • 5
  • 59
  • 74
  • I was under the impression that I didn't need to define a named route for the "Show" part of RESTful routes? Route: `code 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' delete 'order_submissions/:id' => 'order_submissions#destroy' ` – fuzzybabybunny Apr 01 '14 at 16:55
  • Bah, I've added my entire routes to the OP. – fuzzybabybunny Apr 01 '14 at 17:03
  • You do if you want to use a named route to generate a url. You can see, by running `bundle exec rake routes | grep order_submission` that there is no named route auto-generated for you for the :show route. You may be better off not doing this so custom though? I'd just do `resources :order_submissions` instead of defining the 7 routes manually as you seem to be doing. – pdobb Apr 01 '14 at 17:03