控制器中返回文件的名称约定

时间:2013-06-13 02:39:49

标签: ruby-on-rails

假设我有一个位于/admin/users/users_controller.rb的控制器:

class Admin::UsersController < ApplicationController
  def index
    #..........
    respond_to do |format|
      format.html # the file /admin/users/index.html.haml will be returned implicitly 
      format.js   # the file /admin/users/index.js.erb will be returned implicitly 
    end 
  end
end

的routes.rb

 namespace :admin do
  resource :users do
    collection do
     resources :tags, controller: :users_tags
    end
  end
 end

如果我在同一目录中有一个控制器UsersTagsController

class Admin::UsersTagsController < ApplicationController
  def index
     #..........
     respond_to do |format|
       format.html # what file will be returned?
       format.js   # what file will be returned?
     end 
  end
end

那么在这种情况下会返回什么文件?

1 个答案:

答案 0 :(得分:1)

返回的文件/视图将基于控制器的位置而不是路径的格式。例如,它们位于admin/users_tags/index.{format}.erb(假设UsersTagsController位于controllers/admin/users_tags_controller.rb

在旁注中,通常命名空间Admin::UsersController位于controllers/admin/users_controller.rb而不是controllers/admin/users/users_controller.rb