我有这个资源
class Role < ActiveRecord::Base
# lists all the system-level roles
def Role.system_roles
Role.find_all_by_system(true, :order => 'name')
end
# lists all the project-level roles
def Role.project_roles
Role.find_all_by_system(false, :order => 'name')
end
# lists all roles that can be used for workflow enforcement
def Role.filter_roles
not_for_filters = ['admin', 'manager']
Role.project_roles.reject{|role| not_for_filters.include? role.name}
end
end
现在REST API标准说你必须以这种方式返回资源
/roles
就是这样。
在控制器的索引方法中,我只是这样做
module Api::V1::Setup
class RolesController < ApplicationController
include ApiHelper
layout nil
def index
system_roles_data = Role.system_roles
render :json => roles_data, :status => :ok
end
end
end
我看到其他帖子建议传递一个参数。我不想那样做。可以支持以下内容吗?
/roles/system_roles
/roles/project_roles
路线档案中应包含哪些内容?
答案 0 :(得分:1)
如果您在roles_controller.rb
(system_roles
和project_roles
)中创建相应的方法,则可能会出现以下情况:
resources :roles do
collection do
get 'system_roles'
get 'project_roles'
end
end
system_roles
控制器方法将返回Role.system_roles
而project_roles
将分别返回Role.project_roles
。