Rails应用程序文件夹目录结构 [英] rails app folder directory structure

查看:158
本文介绍了Rails应用程序文件夹目录结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Rails项目中的应用程序控制目录





对Rails进行自学,但据我了解,如果我在app文件夹中创建目录,则必须用匹配的路由完成路由文件,例如:



match / editor / usynkdataeditor / saveusynkeditor



向社区提出的问题是,有一种更好的方法可以为特定的工作流程定义不同的目录结构,还是可以安全地在父控制器目录中定义所有控制器。

解决方案

如果在controllers目录中创建其他目录,则实际上是在命名控制器的空间。



因此该控制器将是:

  class Editor :: UsynkdataeditorController< ApplicationController 
def saveusynkeditor
结束
结束

定义后,您可以执行以下操作:

  MyApplication :: Application.routes.draw do 

名称空间:editor do
获得 usynkdataeditor / saveusynkeditor
结束

结束

这将为您提供路线:

  $耙路
editor_usynkdataeditor_saveusynkeditor GET / editor / usynkdataeditor /saveusynkeditor(.:format)编辑器/ usynkdataeditor#saveusynkeditor

或者,最好只使用静态路由而不是saveusynkeditor是这样的:

  MyApplication :: Application.routes.draw do 

namespace:editor do
资源:usynkdataeditor做
收集做
得到:saveusynkeditor
结束
结束
结束

结束

何时您将获得:

  $耙线
saveusynkeditor_editor_usynkdataeditor_index GET /editor/usynkdataeditor/saveusynkeditor(.:format)编辑器/ usynkdataeditor#saveusynkeditor
editor_usynkdataeditor_index编辑器/usynkdataeditor(.:format)编辑器/ usynkdataeditor#index
POST /editor/usynkdataeditor(.:format)编辑器/ usynkdataeditor#create
new_editor_usynkdataeditor GET /editor/usynkdataeditor/new(.:format)编辑器/ usynkdataeditor#new
edit_editor_usynkdataeditor GET /editor/usynkdataeditor/:id/edit(.:format)编辑器/ usynkdataeditor#edit
editor_usynkdataeditor GET /editor/usynkdataeditor/:id(.:format)编辑器/ usynkdataeditor #显示
PUT /编辑器/usynkdataeditor/:id(.:format)编辑器/ usynkdataeditor#update
删除/editor/usynkdataeditor/:id(.:format)编辑器/ usynkdataeditor#destroy

很好的解释 http://guides.rubyonrails.org/routing.html#controller-namespaces-and

最后,回答您的问题:


  1. 更好的方法?嗯,这取决于您的喜好。您如何看待代码的组织方式?您可以使用命名空间,但不必这样做。但是,

  2. 同样,将所有控制器都放在父控制器目录中也没有错。


Here is a app contorller directory from Rails project

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.

这篇关于Rails应用程序文件夹目录结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆