Rails API route 资源错误响应
Rails API route Resources wrong response
我有 Ruby Rails 5.2.4 运行 Ruby 2.6.6。前端期望后端 return 编辑的数据是 JSON 结构化的:“resources”(如果有数据列表)或“recource”(如果只有一个数据)。
该应用程序是为 Rails 5.0 创建的,我进行了一些更新,因此它也可以在 Rails 5.2 上正常运行。也许我错过了什么。
routes.rb 看起来像:
resources :route_a, only: [:index, :show]
resources :route_b, only: [:index]
resources :route_c, only: [:create]
resource :route_d, only: [:create, :update, :destroy]
...
resources :route_e, only: [] do
resource :route_f, only: [] do
post 'route_g', on: :member
end
end
前端 Javascript 框架期望 returned 数据包含“资源”或“资源”键。但在 80% 的情况下,数据没有 return resources/resource 结构化 JSON。 Rails 有什么问题?此问题是否与 gem、数据库有关?为什么对于某些路由,响应是 returned 在 resources/resource 结构化数据中,而对于其他路由,普通 JSON 是 returned?
取而代之的响应如:
[{"id":33,"type":"M..
我需要:
[{"resources":{"id":33,"type":"M...
我是 Rails 的新手,很抱歉问了一些基本的问题,但我不知道这里有什么问题。谢谢。
重要
我有一个文件:config/initializers/wrap_parameters.rb
,其中包含:
# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
ActiveSupport.on_load(:action_controller) do
wrap_parameters format: [:json]
end
# To enable root element in JSON for ActiveRecord objects.
# ActiveSupport.on_load(:active_record) do
# self.include_root_in_json = true
# end
重要 2
这是 /api/integrations
页面控制器:
class IntegrationsController < ApiController
def index
integrations = policy_scope(Integration)
integrations = integrations.where(type: params[:type_eq]) if params[:type_eq]
render_resources integrations, ransack_query: params[:q]
end
end
api/integrations
响应是:
[{"id":254,"name":"Integration 1"...
结果中不包含任何 resources
键。我在下面描述了 render_resources
方法:
def render_resources(resources, options = {})
options[:pagination] = true if options[:pagination].nil?
pagination = options.delete(:pagination)
ransack_query = options.delete(:ransack_query) || {}
ransack_sort = options.delete(:ransack_sort) || params[:sort]
resources = resources.ransack(ransack_query.to_hash.merge(s: ransack_sort)).result if ransack_sort || ransack_query.any?
total = resources.respond_to?(:total_count) ? resources.total_count : resources.length
default = {root: :resources, meta: {total: total}}
results = pagination ? resources.page(params[:page]).per(params[:per_page]) : resources
results = results.includes(options[:includes]) if options[:includes]
render({json: results}.merge(default).merge(options))
end
由于此方法不再 return 包含 resources
键的响应,我可以说这是因为隐含的库 - Ransack 和 Pagination 吗?因为在我更新 ruby 和 rails gem 之前,该功能按预期工作。不过,我不知道应该在 resources
或 resource
键中构建响应的 JSON 数据的库是什么。
如果您直接从 rails 5.0 升级到 5.2,您可能会跳过修复 5.1 中存在的一些弃用警告。 ruby 升级的情况相同 - 除非你有广泛的测试范围并且一切正常 - 最好一次使用一个次要 (5.0->5.1->5.2) 版本。
同样在 rails 升级期间,其他 gem 也会升级,每个 gem 的每个新版本都可能有一些重大更改(或新错误)
如果您使用的是 active_model_serializers - 您可能会受到他们对默认适配器的更改(now it's json_api
) and other movement around root
option, see changelog for 0.10 和以前的更改日志的影响,具体取决于您从
升级的版本
我有 Ruby Rails 5.2.4 运行 Ruby 2.6.6。前端期望后端 return 编辑的数据是 JSON 结构化的:“resources”(如果有数据列表)或“recource”(如果只有一个数据)。
该应用程序是为 Rails 5.0 创建的,我进行了一些更新,因此它也可以在 Rails 5.2 上正常运行。也许我错过了什么。
routes.rb 看起来像:
resources :route_a, only: [:index, :show]
resources :route_b, only: [:index]
resources :route_c, only: [:create]
resource :route_d, only: [:create, :update, :destroy]
...
resources :route_e, only: [] do
resource :route_f, only: [] do
post 'route_g', on: :member
end
end
前端 Javascript 框架期望 returned 数据包含“资源”或“资源”键。但在 80% 的情况下,数据没有 return resources/resource 结构化 JSON。 Rails 有什么问题?此问题是否与 gem、数据库有关?为什么对于某些路由,响应是 returned 在 resources/resource 结构化数据中,而对于其他路由,普通 JSON 是 returned?
取而代之的响应如:
[{"id":33,"type":"M..
我需要:
[{"resources":{"id":33,"type":"M...
我是 Rails 的新手,很抱歉问了一些基本的问题,但我不知道这里有什么问题。谢谢。
重要
我有一个文件:config/initializers/wrap_parameters.rb
,其中包含:
# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
ActiveSupport.on_load(:action_controller) do
wrap_parameters format: [:json]
end
# To enable root element in JSON for ActiveRecord objects.
# ActiveSupport.on_load(:active_record) do
# self.include_root_in_json = true
# end
重要 2
这是 /api/integrations
页面控制器:
class IntegrationsController < ApiController
def index
integrations = policy_scope(Integration)
integrations = integrations.where(type: params[:type_eq]) if params[:type_eq]
render_resources integrations, ransack_query: params[:q]
end
end
api/integrations
响应是:
[{"id":254,"name":"Integration 1"...
结果中不包含任何 resources
键。我在下面描述了 render_resources
方法:
def render_resources(resources, options = {})
options[:pagination] = true if options[:pagination].nil?
pagination = options.delete(:pagination)
ransack_query = options.delete(:ransack_query) || {}
ransack_sort = options.delete(:ransack_sort) || params[:sort]
resources = resources.ransack(ransack_query.to_hash.merge(s: ransack_sort)).result if ransack_sort || ransack_query.any?
total = resources.respond_to?(:total_count) ? resources.total_count : resources.length
default = {root: :resources, meta: {total: total}}
results = pagination ? resources.page(params[:page]).per(params[:per_page]) : resources
results = results.includes(options[:includes]) if options[:includes]
render({json: results}.merge(default).merge(options))
end
由于此方法不再 return 包含 resources
键的响应,我可以说这是因为隐含的库 - Ransack 和 Pagination 吗?因为在我更新 ruby 和 rails gem 之前,该功能按预期工作。不过,我不知道应该在 resources
或 resource
键中构建响应的 JSON 数据的库是什么。
如果您直接从 rails 5.0 升级到 5.2,您可能会跳过修复 5.1 中存在的一些弃用警告。 ruby 升级的情况相同 - 除非你有广泛的测试范围并且一切正常 - 最好一次使用一个次要 (5.0->5.1->5.2) 版本。
同样在 rails 升级期间,其他 gem 也会升级,每个 gem 的每个新版本都可能有一些重大更改(或新错误)
如果您使用的是 active_model_serializers - 您可能会受到他们对默认适配器的更改(now it's json_api
) and other movement around root
option, see changelog for 0.10 和以前的更改日志的影响,具体取决于您从