代码之家  ›  专栏  ›  技术社区  ›  AlbertMunichMar

获取对json api的请求,我得到一个错误:多次调用redirect。我没有重定向

  •  0
  • AlbertMunichMar  · 技术社区  · 6 年前

    从外部应用程序,我向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'
    

    我不明白为什么,我没有重定向。关键是,show和index工作,filter,这不是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
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   AlbertMunichMar    6 年前

    我找到了答案。

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

    all_projects = policy_scope(Project)
    

      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