Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange Rails resource routes behavior

I've met strange error. Im not sure this is bug. However i never met this strange behavior before.

resource :watches

Makes such strange routing table:

        watches POST   /watches(.:format)                         watches#create
    new_watches GET    /watches/new(.:format)                     watches#new
   edit_watches GET    /watches/edit(.:format)                    watches#edit
                GET    /watches(.:format)                         watches#show
                PUT    /watches(.:format)                         watches#update
                DELETE /watches(.:format)                         watches#destroy

As you see no ID param and messed actions

On same time:

resources :mibs

Make proper routes

           mibs GET    /mibs(.:format)                            mibs#index
                POST   /mibs(.:format)                            mibs#create
        new_mib GET    /mibs/new(.:format)                        mibs#new
       edit_mib GET    /mibs/:id/edit(.:format)                   mibs#edit
            mib GET    /mibs/:id(.:format)                        mibs#show
                PUT    /mibs/:id(.:format)                        mibs#update
                DELETE /mibs/:id(.:format)                        mibs#destroy

I thought that is could be somehow inflector problem, but trying using "rockets" instead of "watches" give same result:

        rockets POST   /rockets(.:format)                         rockets#create
    new_rockets GET    /rockets/new(.:format)                     rockets#new
   edit_rockets GET    /rockets/edit(.:format)                    rockets#edit
                GET    /rockets(.:format)                         rockets#show
                PUT    /rockets(.:format)                         rockets#update
                DELETE /rockets(.:format)                         rockets#destroy

Anything except my first two resources (servers and mibs) make such result.

Probably corrupted routing cache somewhere?

like image 274
Igor Yamolov Avatar asked Jan 29 '26 11:01

Igor Yamolov


1 Answers

resource indicates a singleton resource: in other words, you're telling Rails that there's only ever one watch for each user, so passing IDs would be useless.

resources is the standard invocation for getting routes with IDs attached.

So, essentially, the problem is an inflector one, but for resource or resources, not for the name of your routes. For more information, check out the Ruby on Rails routing guide. It does a good job explaining the difference between singleton resources and the more usual kind.

like image 98
Veraticus Avatar answered Jan 31 '26 03:01

Veraticus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!