Ruby 2.6.6에서 실행되는 Ruby on Rails 5.2.4가 있습니다. 프런트 엔드는 백엔드에서 반환 한 데이터가 "resources"(데이터 목록이있는 경우) 또는 "recource"(데이터가 하나만있는 경우)에 구조화 된 JSON이라고 예상합니다.
이 앱은 Rails 5.0 용으로 생성되었으며 일부 업데이트를 수행하여 Rails 5.2에서도 잘 작동합니다. 뭔가 놓친 것 같아요.
route.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
프론트 엔드 자바 스크립트 프레임 워크는 반환 된 데이터에 "리소스"또는 "리소스"키가 포함될 것으로 예상합니다. 그러나 80 %의 경우 데이터가 리소스 / 리소스 구조화 된 JSON을 반환하지 않습니다. Rails에 어떤 문제가 있습니까? 이 문제가 gem, 데이터베이스와 관련이 있습니까? 일부 경로의 경우 응답이 리소스 / 리소스 구조화 된 데이터로 반환되고 다른 경로의 경우 일반 JSON이 반환되는 이유는 무엇입니까?
다음과 같은 응답의 Instad :
[{"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
이 메서드는 더 이상 resources
키를 포함하는 응답을 반환하지 않기 때문에 묵시적 라이브러리 인 Ransack 및 Pagination 때문이라고 말할 수 있습니까? 루비와 레일스 보석을 업데이트하기 전까지는 기능이 예상대로 작동했기 때문입니다. 그래도 응답의 JSON 데이터를 resources
또는 resource
키로 구성해야하는 라이브러리가 무엇인지 모르겠습니다 .
레일 5.0에서 5.2로 바로 이동했다면 5.1에있는 일부 지원 중단 경고 수정을 건너 뛰었을 수 있습니다. 루비 업그레이드와 같은 상황-방대한 테스트 범위가 있고 모든 것이 잘 작동하지 않는 한-한 번에 하나의 마이너 (5.0-> 5.1-> 5.2) 버전을 사용하는 것이 좋습니다.
또한 레일을 업그레이드하는 동안 다른 보석도 업그레이드되며 각 보석의 각 새 버전에는 몇 가지 주요 변경 사항 (또는 새로운 버그)이있을 수 있습니다.
active_model_serializers를 사용하는 경우-기본 어댑터 ( 현재는 json_api
) 및 기타 이동 root
옵션 에 대한 영향을받을 수 있습니다. 업그레이드하는 버전에 따라 0.10 및 이전 변경 로그에 대한 변경 로그를 참조하십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다