Forum: Ruby on Rails Refactoring routes question

Be2b7277cff8516117cbd7a33df7b03a?d=identicon&s=25 Dave Castellano (dcastellano1)
on 2014-03-12 18:34
Hi,

I am wondering if there is any refactoring possible in the following
routes as there seems to be a lot of redundancy in the code.  I am
learning on my own so the answer may be obvious and I appreciate the
help.  (... all the routes are currently working fine)  Rails 4.0.3


# Routes for navigating contents:
 #Books
  get 'contents/index_books/:subject_id',                   to:
'contents#index_books',            :as => :index_books
  get 'contents/:id/edit_book',                             to:
"contents#edit_book",              :as => :edit_book
  patch 'contents/:id/update_book',                         to:
"contents#update_book",            :as => :update_book
  get 'contents/:subject_id/new_book',                      to:
"contents#new_book",               :as => :new_book
  post 'contents/create_book',                              to:
"contents#create_book",            :as => :create_book
  delete 'contents/:id/destroy_book',                       to:
"contents#destroy_book",           :as => :destroy_book

 #Chapters
  get 'contents/index_chapters/:book_id',                   to:
'contents#index_chapters',         :as => :index_chapters
  get 'contents/:id/edit_chapter',                          to:
"contents#edit_chapter",           :as => :edit_chapter
  patch 'contents/:id/update_chapter',                      to:
"contents#update_chapter",         :as => :update_chapter
  get 'contents/:book_id/new_chapter',                      to:
"contents#new_chapter",            :as => :new_chapter
  post 'contents/create_chapter',                           to:
"contents#create_chapter",         :as => :create_chapter
  delete 'contents/:id/destroy_chapter',                    to:
"contents#destroy_chapter",        :as => :destroy_chapter

 #Sections
  get 'contents/index_sections/:chapter_id',                to:
"contents#index_sections",         :as => :index_sections
  get 'contents/:id/edit_section',                          to:
"contents#edit_section",           :as => :edit_section
  patch 'contents/:id/update_section',                      to:
"contents#update_section",         :as => :update_section
  get 'contents/:chapter_id/new_section',                   to:
"contents#new_section",            :as => :new_section
  post 'contents/create_section',                           to:
"contents#create_section",         :as => :create_section
  delete 'contents/:id/destroy_section',                    to:
"contents#destroy_section",        :as => :destroy_section

 #Subsections
  get 'contents/index_subsections/:section_id',             to:
"contents#index_subsections",      :as => :index_subsections
  get 'contents/:id/edit_subsection',                       to:
"contents#edit_subsection",        :as => :edit_subsection
  patch 'contents/:id/update_subsection',                   to:
"contents#update_subsection",      :as => :update_subsection
  get 'contents/:section_id/new_subsection',                to:
"contents#new_subsection",         :as => :new_subsection
  post 'contents/create_subsection',                        to:
"contents#create_subsection",      :as => :create_subsection
  delete 'contents/:id/destroy_subsection',                 to:
"contents#destroy_subsection",     :as => :destroy_subsection

 #Minisections
  get 'contents/index_minisections/:subsection_id',         to:
"contents#index_minisections",     :as => :index_minisections
  get 'contents/:id/edit_minisection',                      to:
"contents#edit_minisection",       :as => :edit_minisection
  patch 'contents/:id/update_minisection',                  to:
"contents#update_minisection",     :as => :update_minisection
  get 'contents/:subsection_id/new_minisection',            to:
"contents#new_minisection",        :as => :new_minisection
  post 'contents/create_minisection',                       to:
"contents#create_minisection",     :as => :create_minisection
  delete 'contents/:id/destroy_minisection',                to:
"contents#destroy_minisection",    :as => :destroy_minisection

Thanks,
Dave
B2ea0545dc3b54e877f4a36be4fcde89?d=identicon&s=25 Gustavo Caso (Guest)
on 2014-03-18 11:10
(Received via mailing list)
El miércoles, 12 de marzo de 2014 18:34:51 UTC+1, Ruby-Forum.com User
escribió:
>  #Books
>   delete 'contents/:id/destroy_book',                       to:
> "contents#new_chapter",            :as => :new_chapter
>   patch 'contents/:id/update_section',                      to:
> "contents#index_subsections",      :as => :index_subsections
>
> "contents#create_minisection",     :as => :create_minisection
>   delete 'contents/:id/destroy_minisection',                to:
> "contents#destroy_minisection",    :as => :destroy_minisection
> Dave
>
> --
> Posted via http://www.ruby-forum.com/.
>

You can use the scope with contents, so it will refactor I bit not much,
but a least is something.

scope 'contents' do
  get 'index_books/:subject_id', to: 'contents#index_books',  :as =>
:index_books
  get ':id/edit_book',                to: "contents#edit_book",
 :as => :edit_book
  patch ':id/update_book',             to: "contents#update_book",
   :as => :update_book
  get ':subject_id/new_book',          to: "contents#new_book",
  :as => :new_book
  post 'create_book',                  to: "contents#create_book",
   :as => :create_book
  delete ':id/destroy_book',           to: "contents#destroy_book",
  :as => :destroy_book
  get ':id/edit_chapter',              to: "contents#edit_chapter",
  :as => :edit_chapter
  patch ':id/update_chapter',          to: "contents#update_chapter",
  :as => :update_chapter
  get ':book_id/new_chapter',          to: "contents#new_chapter",
   :as => :new_chapter
  post 'create_chapter',               to: "contents#create_chapter",
  :as => :create_chapter
  delete ':id/destroy_chapter',        to: "contents#destroy_chapter",
   :as => :destroy_chapter
  get 'index_sections/:chapter_id',    to: "contents#index_sections",
  :as => :index_sections
  get ':id/edit_section',              to: "contents#edit_section",
  :as => :edit_section
  patch ':id/update_section',          to: "contents#update_section",
  :as => :update_section
  get ':chapter_id/new_section',       to: "contents#new_section",
   :as => :new_section
  post 'create_section',               to: "contents#create_section",
  :as => :create_section
  delete ':id/destroy_section',        to: "contents#destroy_section",
   :as => :destroy_section
  get 'index_subsections/:sectio_d',   to: "contents#index_subsections",
   :as => :index_subsections
  get ':id/edit_subsection',           to: "contents#edit_subsection",
   :as => :edit_subsection
  patch ':id/update_subsection',       to: "contents#update_subsection",
   :as => :update_subsection
  get ':section_id/new_subsection',    to: "contents#new_subsection",
  :as => :new_subsection
  post 'create_subsection',            to: "contents#create_subsection",
   :as => :create_subsection
  delete ':id/destroy_subsection',     to:
"contents#destroy_subsection",
  :as => :destroy_subsection
  get 'index_minisections/:subscion_id'to:
"contents#index_minisections",
  :as => :index_minisections
  get ':id/edit_minisection',          to: "contents#edit_minisection",
  :as => :edit_minisection
  patch ':id/update_minisection',      to:
"contents#update_minisection",
  :as => :update_minisection
  get ':subsection_id/new_minisection',to: "contents#new_minisection",
   :as => :new_minisection
  post 'create_minisection',           to:
"contents#create_minisection",
  :as => :create_minisection
  delete ':id/destroy_minisection',    to:
"contents#destroy_minisection",
   :as => :destroy_minisection
end

  get 'chapters/index_chapters/:book_id', to:
'contents#index_chapters',:as
=> :index_chapters

Hope this help you.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.