rails app folder directory structure

℡╲_俬逩灬. 提交于 2019-12-05 17:35:10

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!