Ruby on rails Rails中抽象资源的JSON API资源控制器
我正在为我的Rails API使用Jsonapi资源。我有一个Ruby on rails Rails中抽象资源的JSON API资源控制器,ruby-on-rails,jsonapi-resources,Ruby On Rails,Jsonapi Resources,我正在为我的Rails API使用Jsonapi资源。我有一个用户模型,它有一个布尔值帐户管理器。我想为客户经理提供一个单独的控制器/端点 索引操作应返回作为帐户管理员的所有用户,并清理数据,以仅返回id、名字和姓氏 我有一个AccountManagerResource和AccountManagerController。但是由于@model在我的资源中为零,索引操作错误。有人知道我遗漏了什么吗 class JsonApiBaseResource < JSONAPI::Resource
用户
模型,它有一个布尔值帐户管理器
。我想为客户经理提供一个单独的控制器/端点
索引操作应返回作为帐户管理员的所有用户,并清理数据,以仅返回id
、名字和姓氏
我有一个AccountManagerResource
和AccountManagerController
。但是由于@model
在我的资源中为零,索引操作错误。有人知道我遗漏了什么吗
class JsonApiBaseResource < JSONAPI::Resource
abstract
relationship :customer, to: :one
before_create do
@model.customer_id = context[:current_customer].id
end
filter :updated_since,
verify: -> (value, _context) {
value.map{ |value| value.to_i }
},
apply: -> (records, value, _options) {
records.updated_since(Time.at(value[0]))
}
def self.records(options = {})
current_customer(options).public_send(@model.to_s.tableize).ordered
end
def self.current_customer(options)
options[:context][:current_customer]
end
end
classjsonapibaseresource(值,_上下文){
value.map{| value | value.to_i}
},
应用:->(记录、值、选项){
记录。自(时间点(值[0])起更新了\u
}
def self.records(选项={})
当前客户(选项)。公共发送(@model.to_.s.tableze)。已订购
结束
def自身当前客户(选项)
选项[:上下文][:当前客户]
结束
结束
用户资源
class UserResource < JsonApiBaseResource
attributes :title, :first_name, :last_name, :email, :company_name,
:job_title, :telephone_number, :user_type
relationship :account_manager, to: :one
relationship :company, to: :one
relationship :roles, to: :many
relationship :asset_permissions, to: :many
paginator :paged_with_all
before_create do
if current_user = context[:current_user]
@model.created_by_admin = current_user.super_admin?
end
end
filter :search,
apply: -> (records, value, _options) {
records.search(value[0])
}
filter :user_type
filter :roles
filter :company
filter :account_manager
end
class UserResource(记录、值、选项){
记录。搜索(值[0])
}
过滤器:用户类型
过滤器:角色
过滤器:公司
筛选器:帐户管理器
结束
客户经理资源
class AccountManagerResource < JsonApiBaseResource
abstract
attributes :id, :first_name, :last_name
model_name 'User'
end
class AccountManagerResource
客户经理兼财务总监
class AccountManagersController < JsonApiBaseController
apipie_concern_subst(resource: 'Account Manger')
include CrudActions
skip_before_action :authenticate_user, only: [:index]
resource_description do
description <<-EOS
- **type** = users
### Attributes
- **id**: *number*
- **first-name**: *string*
- **last-name**: *string*
EOS
end
api! "Returns all Account Manager names"
def index
super
end
end
class AccountManagersController 描述我认为您的问题是JsonApiBaseResource文件中的before操作。在索引方法@model.customer\u id=etc之前设置了一个before操作。。。但是您在索引操作之前定义了什么是实例变量模型吗?我认为您的问题是JsonApiBaseResource文件中的BEFORE操作。在索引方法@model.customer\u id=etc之前设置了一个before操作。。。但是,在索引操作之前,您是否定义了实例变量模型?