skip to Main Content

I’ve this on routes.rb

resources :questions, except: [:show] do
   get '/resource/:subject/:id', to: 'resource#show', as: "resource", param: [:name, :id]

It says that:

Invalid route name, already in use: ‘resource’ You may have defined two routes with the same name using the :as option, or you may be overriding a route already defined by a resource with the same naming

I know that resources create two routes with the same path, show and destroy both uses resource_path, how does it is being created internally? and how i can generate my route for show wihtout overwrite the one in destroy?

2

Answers


  1. It seems to me that you could take out show and then define the route that you want separately. See if this works:

    resources :questions, except: :show
    
    get '/resource/:subject/:id',
      to: 'resource#show',
      as: "resource",  # This is where the error is.
      param: [:name, :id]
    

    EDIT: Ah, yes. The :as parameter needs a different name. This will work:

    resources :questions, except: :show
    
    get '/resource/:subject/:id',
      to: 'resource#show',
      as: "resource_show",
      param: [:name, :id]
    
    Login or Signup to reply.
  2. A good way to eliminate routes unneeded is by specifying the :only option

    resources :user, :only => [:edit] 
    

    instead of

    resources :user, :except => [:new, :create, :edit, :update, :show, :destroy]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search