简体   繁体   中英

rails app folder directory structure

Here is a app contorller directory from Rails project

** app控制器目录**

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.

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.

This falls under Namespacing and it's generally considered the best approach to do what you're trying to do. Check it out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM