madeindjs/api_on_rails

Chapter 2 config/routes.rb listing 4-6 changes

notapatch opened this issue · 3 comments

One of the confusing things in Chapter 2 is the block inside the namespace :api

Listing 4. config/routes.rb

Rails.application.routes.draw do
  namespace :api, defaults: { format: :json } do
    namespace :v1 do                   <= This guy
      # We are going to list our resources here
    end
  end
end

You then commit as "Set the routes constraints for the api". So far no problem. Then we see.

Listing 5. config/routes.rb

Rails.application.routes.draw do
  # Api definition
  namespace :api, defaults: { format: :json }  do
    # We are going to list our resources here           <= where's it gone?
  end
end

This is a bit confusing as the version namespace :v1 has disappeared. Maybe it's because it wasn't interesting? Maybe because it's a mistake? However, In Listing 6 we have namespace changed to scope.

Listing 6. config/routes.rb

Rails.application.routes.draw do
  # Api definition
  namespace :api, defaults: { format: :json }  do
    scope module: :v1 do            <= so we changed namespace to scope?
      # We are going to list our resources here
    end
  end
end

Is the progression from Listing 4 (adding an inner namespace), Listing 5 (missing it out again) and Listing 6 (inner scope module) deliberate? It's quite confusing to follow and not clear from the text why these changes happened.

Thanks do report. I think it was an old code from API on Rails 5 version. In this version I prefered to use namespace. You can find the final code at https://github.com/madeindjs/market_place_api_6/blob/master/config/routes.rb .

I will make a proofreading of chapter 2. Thanks again

Thanks for the link. I'll check that out tomorrow.

Effectively I made some mistake when updated book. I think this commit correct misunderstanding: a9c1abc. Thanks for your report.