user3461645
user3461645

Reputation: 195

Rails Routes Named Helper

I am coming across an odd problem, and am not sure how to correct. I initially created a resource in my application with the full REST actions. Once I started cleaning up the code, I realized I didn't need all of the actions, and changed the routes code to reflect that. After doing that, it changed some of the Named Helpers around so that the "index" path is pointing to the create action and the helper path that was pointing to show now points to update. I have made sure that the index_path is not being used in any of the code. Is there a way to cleanly remove the actions that are no longer needed?

Original Code:

  resources :users do
    resources :license
  end

New Code:

  resources :users do
    resources :license, only: [:new, :create, :edit, :update, :destroy]
  end

Original Routes List for license:

user_license_index GET    /users/:user_id/license(.:format)                    license#index
                   POST   /users/:user_id/license(.:format)                    license#create
  new_user_license GET    /users/:user_id/license/new(.:format)                license#new
 edit_user_license GET    /users/:user_id/license/:id/edit(.:format)           license#edit
      user_license GET    /users/:user_id/license/:id(.:format)                license#show
                   PATCH  /users/:user_id/license/:id(.:format)                license#update
                   PUT    /users/:user_id/license/:id(.:format)                license#update
                   DELETE /users/:user_id/license/:id(.:format)                license#destroy

New Routes after removing index and show:

user_license_index POST   /users/:user_id/license(.:format)                    license#create
  new_user_license GET    /users/:user_id/license/new(.:format)                license#new
 edit_user_license GET    /users/:user_id/license/:id/edit(.:format)           license#edit
      user_license PATCH  /users/:user_id/license/:id(.:format)                license#update
                   PUT    /users/:user_id/license/:id(.:format)                license#update
                   DELETE /users/:user_id/license/:id(.:format)                license#destroy

Upvotes: 2

Views: 822

Answers (1)

Ryan K
Ryan K

Reputation: 4053

Ok, so I understand what your question is. Here's the issue: user_license_index doesn't just point to the index action; it ALSO points to the create action. The difference is whether you use a GET or POST request (GET by default). However, the routes page only lists the helper once. So when you remove the index action, it removes the index route from the list so now user_license_index only points to the create action.

The same idea goes for user_license. It points to the show, update, and destroy actions. The difference is if you use a GET, PATCH/PUT, or DELETE request.

BTW, I think the reason why there is an _index suffix is that the word "license" is a singular-only word (just like "sheep" or "fish").

Upvotes: 1

Related Questions