Coder Social home page Coder Social logo

space_cats's Introduction

README

Routes Recap:

Let's add some routes to our routes.rb for cats that will show all cats, show one cat, delete a cat, and edit a cat.

    get '/cats', to: 'cats#index'
    get '/cats/:id', to: 'cats#show'
    patch '/cats', to: 'cats#update'
    get '/cats/edit', to: 'cats#edit'
    delete '/cats/:id', to: 'cats#destroy'

At this point, when we run rake routes, we get the following:

   Prefix Verb   URI Pattern          Controller#Action
     cats GET    /cats(.:format)      cats#index
          GET    /cats/:id(.:format)  cats#show
          PATCH  /cats(.:format)      cats#update
cats_edit GET    /cats/edit(.:format) cats#edit
          DELETE /cats/:id(.:format)  cats#destroy

Distinguishing Routes

Let's say we have:

  • cats
  • administrators

We want a way to distinguish your routes so an admin has additional functionality/control over your application.

For example, say we want http://localhost:3000/admin/cats to show edit/delete buttons for each individual cat and only admins can get here.

We also want http://localhost:3000/cats to show a list of cats (and anyone visiting our application can get here).

What can we do?

Scope

	# config/routes.rb
        get '/cats', to: 'cats#index'
        get '/cats/:id', to: 'cats#show'
	scope :admin do
	      patch '/cats', to: 'cats#update'
   	      get '/cats/edit', to: 'cats#edit'
    	      delete '/cats/:id', to: 'cats#destroy'
	end

Adding scope to our routes gives us the following when we run rake routes:

   Prefix Verb   URI Pattern                Controller#Action
     cats GET    /cats(.:format)            cats#index
          GET    /cats/:id(.:format)        cats#show
          PATCH  /admin/cats(.:format)      cats#update
cats_edit GET    /admin/cats/edit(.:format) cats#edit
          DELETE /admin/cats/:id(.:format)  cats#destroy

Potential Problems with scope

We're going to need a way to differentiate our controllers. We want what we already have (the url prefix) AND a separate controller to encapsulate the different functionality.

We want both /admin/cats and /cats to be handled by our controllers in different ways.

Scope and Module

	get '/cats', to: 'cats#index'
        get '/cats/:id', to: 'cats#show'
	scope :admin, module: :admin do
	 patch '/cats', to: 'cats#update'
   	 get '/cats/edit', to: 'cats#edit'
    	 delete '/cats/:id', to: 'cats#destroy'
	end

If we have scope with module in our routes, we will get the following rake routes output:

     cats GET    /cats(.:format)            cats#index
          GET    /cats/:id(.:format)        cats#show
          PATCH  /admin/cats(.:format)      admin/cats#update
cats_edit GET    /admin/cats/edit(.:format) admin/cats#edit
          DELETE /admin/cats/:id(.:format)  admin/cats#destroy

By using module, Rails looks for our controller in a different place.

	# When we hit "http://localhost3000/admin/cats"

	# app/controllers/admin/cats_controller.rb
	class Admin::CatsController < ApplicationController
 	 def index
	  @cats = Cat.all
	 end
	end

What does that :: (scope resolution operator) remind us of?

Note: Where do you think Rails will look for this view template? It will look in the views/admin/cats folder.

Recap

  • What have we done so far to our routes?
  • What did module change for us?
  • Do you notice anything missing when you run rake routes?

As you may have noticed, we don't have any path helpers that are specific to this "special" admin prefix. Again, Rails can help us out with this.

scope, module and as

	get '/cats', to: 'cats#index'
    	get '/cats/:id', to: 'cats#show'
	scope :admin, module: :admin, as: :admin do
         patch '/cats', to: 'cats#update'
   	 get '/cats/edit', to: 'cats#edit'
    	 delete '/cats/:id', to: 'cats#destroy'
	end

Let's run rake routes once again!

         Prefix Verb   URI Pattern                Controller#Action
           cats GET    /cats(.:format)            cats#index
                GET    /cats/:id(.:format)        cats#show
     admin_cats PATCH  /admin/cats(.:format)      admin/cats#update
admin_cats_edit GET    /admin/cats/edit(.:format) admin/cats#edit
          admin DELETE /admin/cats/:id(.:format)  admin/cats#destroy

So what does using scope, module, and as provide for us?

  • path helpers via the prefix (admin_cats_path)
  • controller prefix (Admin::CatsController) for more organization
  • url prefix for user's to see in their browser (http://localhost:3000/admin/cats)

As you may have expected, this seems like a lot of work for something that's used quite often. Rails actually makes this even easier for us.

Namespace

namespace = scope + module + as

Rad!

Update the routes file to the following:

	get '/cats', to: 'cats#index'
    	get '/cats/:id', to: 'cats#show'
	namespace :admin do
	 patch '/cats', to: 'cats#update'
   	 get '/cats/edit', to: 'cats#edit'
    	 delete '/cats/:id', to: 'cats#destroy'
	end

vs

        get '/cats', to: 'cats#index'
    	get '/cats/:id', to: 'cats#show'
	scope :admin, module: :admin, as: :admin do
	 patch '/cats', to: 'cats#update'
   	 get '/cats/edit', to: 'cats#edit'
    	 delete '/cats/:id', to: 'cats#destroy'
	end

Why should we use namespace, scope, module, or as?

  • readability
  • organization
  • specificity

Can you imagine what happens when you have 400 lines in your routes file?! You'll be thankful these route blocks exist for organization alone.

Test Your Understanding

By the end of this work period have a written response to each of the following questions.

  • Describe what each of the following things does in the context of our routes file:
    • scope
    • module
    • as
    • namespace
  • Why might it be beneficial to have two controllers for Songs (one in controllers/admin and one just in controllers)? Would it have any downsides?
  • What about different routes? Would we ever want to have /admin/songs and /songs? Why or why not?

space_cats's People

Contributors

megstang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.