Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails app folder directory structure

Here is a app contorller directory from Rails project

**app controller directory**

doing a self study for rails, but from what I understand if I create a directory in the app folder then I have to do the complete the routes files with a match that route like:

match "/editor/usynkdataeditor/saveusynkeditor",

Question to the community is there a better way that I can define different directory structure for a specific workflow or is it safe to define all the controllers in parent controllers directory.

like image 386
sathish_at_madison Avatar asked Dec 27 '22 03:12

sathish_at_madison


1 Answers

If you create additional directory in controllers directory, you are effectively namespacing your controllers.

So this controller would be:

class Editor::UsynkdataeditorController < ApplicationController
  def saveusynkeditor
  end
end

As far as routes are defined, you can do something like:

MyApplication::Application.routes.draw do

  namespace :editor do
    get "usynkdataeditor/saveusynkeditor"
  end

end

Whish will give you route:

$ rake routes
editor_usynkdataeditor_saveusynkeditor GET /editor/usynkdataeditor/saveusynkeditor(.:format) editor/usynkdataeditor#saveusynkeditor

Or, preferably just use restful routes instead of saveusynkeditor like this:

MyApplication::Application.routes.draw do

  namespace :editor do
    resources :usynkdataeditor do
      collection do
        get :saveusynkeditor
      end
    end
  end

end

when you will get:

$ rake routes
saveusynkeditor_editor_usynkdataeditor_index GET    /editor/usynkdataeditor/saveusynkeditor(.:format) editor/usynkdataeditor#saveusynkeditor
                editor_usynkdataeditor_index GET    /editor/usynkdataeditor(.:format)                 editor/usynkdataeditor#index
                                             POST   /editor/usynkdataeditor(.:format)                 editor/usynkdataeditor#create
                  new_editor_usynkdataeditor GET    /editor/usynkdataeditor/new(.:format)             editor/usynkdataeditor#new
                 edit_editor_usynkdataeditor GET    /editor/usynkdataeditor/:id/edit(.:format)        editor/usynkdataeditor#edit
                      editor_usynkdataeditor GET    /editor/usynkdataeditor/:id(.:format)             editor/usynkdataeditor#show
                                             PUT    /editor/usynkdataeditor/:id(.:format)             editor/usynkdataeditor#update
                                             DELETE /editor/usynkdataeditor/:id(.:format)             editor/usynkdataeditor#destroy

There is a really good explanation http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing of what you are trying to achieve in rails guides.

Finally, to answer your question:

  1. Better way? Well it's up to your preferences. How do you like your code organized? You can use namespacing but you don't have to. However,
  2. at the same there is nothing wrong with having all controllers in parent controller directory.
like image 106
Krule Avatar answered Jan 05 '23 10:01

Krule