首页 > 解决方案 > 获取对 json api 的请求,我得到错误:多次调用重定向。我没有重定向

问题描述

从外部应用程序,我向 api 发出 get 请求:

  def index
    user_id = params["user_id"].to_i
    api_url = "#{Rails.configuration.zaina_platform_path}/api/v1/users/#{user_id}/projects.json" 
    html_file = open(api_url).read

    @deal_rooms = JSON.parse(html_file)
  end

我最终找到了正确的方法(我的 api 的终点),并且成功过滤了数组:

  def filter
    user_id = params["user_id"].to_i
    guest_user = User.find(user_id)
    all_projects = policy_scope(Project)

    @user_projects = user_projects(all_projects, guest_user)
  end

然后,我在视图中创建 JSON 响应:

# app/views/api/v1/projects/filter.json.jbuilder
json.array! @user_projects do |project|
  json.extract! project, :id, :name, :description
end

不幸的是,当我提出请求时,我收到以下错误消息:

AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".):

app/controllers/api/v1/base_controller.rb:30:in `internal_server_error'

我不知道为什么,我没有重定向。关键是,显示和索引工作,过滤器,这不是rails方法,它不起作用。有任何想法吗?

完整的日志错误如下:

Started GET "/api/v1/users/1/projects.json" for 127.0.0.1 at 2018-07-19 22:28:13 +0200
Processing by Api::V1::ProjectsController#filter as JSON
  Parameters: {"user_id"=>"1"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Project Load (0.6ms)  SELECT "projects".* FROM "projects"
  ProjectMember Load (0.2ms)  SELECT "project_members".* FROM 
  CACHE EntrepreneurProfile Load (0.0ms)  SELECT  "entrepreneur_profiles".* FROM "entrepreneur_profiles" WHERE "entrepreneur_profiles"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  CACHE User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
  Rendering api/v1/projects/filter.json.jbuilder
  Rendered api/v1/projects/filter.json.jbuilder (0.6ms)
Completed 500 Internal Server Error in 152ms (Views: 4.3ms | ActiveRecord: 4.9ms)



AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".):

app/controllers/api/v1/base_controller.rb:30:in `internal_server_error'

和路线

Rails.application.routes.draw do
  devise_for :users, controllers: { sessions: "sessions" }
  # devise_for :users
  root to: 'pages#home'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

  namespace :api, defaults: { format: :json } do
    namespace :v1 do
      get "users/:user_id/projects", to: "projects#filter"

      resources :projects, only: [ :show, :index ]
      # get "/api/v1/projects/"
    end
  end

end

标签: ruby-on-railsapirequest

解决方案


我找到了答案。

在我的过滤方法中,我正在使用:

all_projects = policy_scope(Project)

所以最后它就像索引方法一样,我需要非常政策范围而不是权威人士的方法 verify_authorized

  after_action :verify_policy_scoped, only: [:index, :filter] ###

这是一个权威问题。该错误完全具有误导性,它不是重定向问题。

我发现它检查了我的 base_controller.rb 中的异常

  def internal_server_error(exception)
    if Rails.env.development?
      response = { type: exception.class.to_s, message: exception.message, backtrace: exception.backtrace }
    else
      response = { error: "Internal Server Error" }
    end
    render json: response, status: :internal_server_error
  end

在这里,我可以阅读真正的错误,即身份验证问题,而不是重定向问题。


推荐阅读