Rails application folder structure

Here is the contorller directory of the application from the Rails project

** app controller directory **

do my own research for rails, but from what I understand, if I create a directory in the app folder, then I have to execute all route files that match this route:

match "/editor/usynkdataeditor/saveusynkeditor"

,

A question to the community is the best way to define the directory structure for a specific workflow, or safely define all controllers in the parent controllers directory.

+3


source to share


2 answers


If you create an additional directory in the controllers directory, you are actually using your controllers namespace.

So this controller will:

class Editor::UsynkdataeditorController < ApplicationController
  def saveusynkeditor
  end
end

      

In terms of routes, you can do something like:

MyApplication::Application.routes.draw do

  namespace :editor do
    get "usynkdataeditor/saveusynkeditor"
  end

end

      

Which route will give you:

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

      



Or, it is advisable to just use quiet 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 receive:

$ 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 at 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:

  • The best way? Well, that depends on your preference. How do you like your code? You can use a namespace, but you don't need it. Nevertheless,
  • At the same time, there is nothing wrong with having all controllers in the parent controller directory.
+8


source


This falls under Namespacing and is generally considered the best approach to what you are trying to do. Check it.



+1


source







All Articles