Rresouceful Routing Better Practice

# Rresouceful Routing Practice
## About this documents
This document is my memo for better routing ( not saying 'best practice' because I do not know this is ).
I hope this could help some people lost on Routing.

# Routing
## Basic Routing
> app/controller/main1_controller.rb

```rb
# in routes.rb
resources :main1
```

         main1_index GET    /main1(.:format)           main1#index
                     POST   /main1(.:format)           main1#create
           new_main1 GET    /main1/new(.:format)       main1#new
          edit_main1 GET    /main1/:id/edit(.:format)  main1#edit
               main1 GET    /main1/:id(.:format)       main1#show
                     PATCH  /main1/:id(.:format)       main1#update
                     PUT    /main1/:id(.:format)       main1#update
                     DELETE /main1/:id(.:format)       main1#destroy

## Add Some Method to Routing
> app/controller/main2_controller.rb

```rb
# in routes.rb
resources :main2 do
  # member means this routing needs id
  # so this is going to be "main/:id/share"
  member do
    # METHOD 'ACTION NAME'
    get 'share'
  end
  # Above is equal with:
  get 'share', on: :member

  # collection means this routing doesn't need id
  # so this is going to be "main/check"
  collection do
    get 'check'
  end
end
```
```
         share_main2 GET    /main2/:id/share(.:format) main2#share
   check_main2_index GET    /main2/check(.:format)     main2#check
         main2_index GET    /main2(.:format)           main2#index
                     POST   /main2(.:format)           main2#create
           new_main2 GET    /main2/new(.:format)       main2#new
          edit_main2 GET    /main2/:id/edit(.:format)  main2#edit
               main2 GET    /main2/:id(.:format)       main2#show
                     PATCH  /main2/:id(.:format)       main2#update
                     PUT    /main2/:id(.:format)       main2#update
                     DELETE /main2/:id(.:format)       main2#destroy

```

## Change Controller for Routing
> app/controller/main3_controller.rb

```rb
# in routes.rb
resources :changed, controller: :main3
```

```
       changed_index GET    /changed(.:format)          main3#index
                     POST   /changed(.:format)          main3#create
         new_changed GET    /changed/new(.:format)      main3#new
        edit_changed GET    /changed/:id/edit(.:format) main3#edit
             changed GET    /changed/:id(.:format)      main3#show
                     PATCH  /changed/:id(.:format)      main3#update
                     PUT    /changed/:id(.:format)      main3#update
                     DELETE /changed/:id(.:format)      main3#destroy
```

## Nested Routing and Change Path
> app/controller/user/main4_controller.rb

```rb
# in routes.rb
## Pattern1 change controller path
resources :user, controller: "user/main4"

## Pattern2 put inside module and change path
resources :main4, module: :user, path: 'user'

## Pattern3 inside namespace and change path to ''
namespace :user do
  resources :main4, path: ''
end
```

```
## Pattern1
       user_index GET    /user(.:format)               user/main4#index
                  POST   /user(.:format)               user/main4#create
         new_user GET    /user/new(.:format)           user/main4#new
        edit_user GET    /user/:id/edit(.:format)      user/main4#edit
             user GET    /user/:id(.:format)           user/main4#show
                  PATCH  /user/:id(.:format)           user/main4#update
                  PUT    /user/:id(.:format)           user/main4#update
                  DELETE /user/:id(.:format)           user/main4#destroy
```

```
## Pattern2
      main4_index GET    /user(.:format)               user/main4#index
                  POST   /user(.:format)               user/main4#create
        new_main4 GET    /user/new(.:format)           user/main4#new
       edit_main4 GET    /user/:id/edit(.:format)      user/main4#edit
            main4 GET    /user/:id(.:format)           user/main4#show
                  PATCH  /user/:id(.:format)           user/main4#update
                  PUT    /user/:id(.:format)           user/main4#update
                  DELETE /user/:id(.:format)           user/main4#destroy
```

```
## Pattern3
 user_main4_index GET    /user(.:format)             user/main4#index
                  POST   /user(.:format)             user/main4#create
   new_user_main4 GET    /user/new(.:format)         user/main4#new
  edit_user_main4 GET    /user/:id/edit(.:format)    user/main4#edit
       user_main4 GET    /user/:id(.:format)         user/main4#show
                  PATCH  /user/:id(.:format)         user/main4#update
                  PUT    /user/:id(.:format)         user/main4#update
                  DELETE /user/:id(.:format)         user/main4#destroy
```

Module option changes only controller action path.