Shawn Wilson
Shawn Wilson

Reputation: 1351

Issue with Routes when implementing a nested form

I am building out a CAD App with Rails 4 Ruby 2

I have a Call model and a Update model and I am beginning the process to nest the Update into my call.

I have already completed the views and controller nesting etc but I seem to be hung up on my Routes.rb.

The error I am getting is:

Can't use collection outside resource(s) scope

My Routes.rb file looks like:

  resources :calls do 
      resources :updates, except: [:index], controller: 'calls/updates'
    end
      collection do
        get 'history'
      end
      member do
        patch :update_unit_on_scene
        patch :update_unit_clear
        patch :update_unit2_os
        patch :update_unit2_cl
        patch :update_unit3_os
        patch :update_unit3_cl
        patch :update_unit4_os
        patch :update_unit4_cl
      end

I am very new with Nested forms / views etc so I am thinking this is where I am going wrong.

My Full Routes File:

Rails.application.routes.draw do

  devise_for :users, controllers: { registrations: 'registrations' }

  devise_scope :user do
    authenticated :user do
      root 'calls#index', as: :authenticated_root
    end

    unauthenticated do
      root 'devise/sessions#new', as: :unauthenticated_root
    end
  end

  resources :sites

  resources :calls do 
      resources :updates, except: [:index], controller: 'calls/updates'
    end
      collection do
        get 'history'
      end
      member do
        patch :update_unit_on_scene
        patch :update_unit_clear
        patch :update_unit2_os
        patch :update_unit2_cl
        patch :update_unit3_os
        patch :update_unit3_cl
        patch :update_unit4_os
        patch :update_unit4_cl
      end

end

Upvotes: 2

Views: 60

Answers (1)

Zoran
Zoran

Reputation: 4226

Your routes file seems to have a misplaced end keyword. Try writing your routes file like so:

Rails.application.routes.draw do

  devise_for :users, controllers: { registrations: 'registrations' }

  devise_scope :user do
    authenticated :user do
      root 'calls#index', as: :authenticated_root
    end

    unauthenticated do
      root 'devise/sessions#new', as: :unauthenticated_root
    end
  end

  resources :sites

  resources :calls do 
    resources :updates, except: [:index], controller: 'calls/updates'

  # end keyword was placed here initially, which was closing off 
  # the resource scope for the routes being defined below,
  # causing the error you were seeing

    collection do
      get 'history'
    end

    member do
      patch :update_unit_on_scene
      patch :update_unit_clear
      patch :update_unit2_os
      patch :update_unit2_cl
      patch :update_unit3_os
      patch :update_unit3_cl
      patch :update_unit4_os
      patch :update_unit4_cl
    end

  # moving end keyword to this position ensures that the calls resource
  # properly encloses the collection and member routes

  end
end

Hope it helps!

Upvotes: 2

Related Questions