Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails JSON Rest API:通过API调用从父类获取子类_Ruby On Rails_Json_Rest_Has Many_Belongs To - Fatal编程技术网

Ruby on rails Rails JSON Rest API:通过API调用从父类获取子类

Ruby on rails Rails JSON Rest API:通过API调用从父类获取子类,ruby-on-rails,json,rest,has-many,belongs-to,Ruby On Rails,Json,Rest,Has Many,Belongs To,我的Rails应用程序中有两个API控制器用于RESTful设置: StoresController(有许多产品) ProductsController(有一个存储) 我如何编写API以便 仅返回该门店的产品(在本例中为门店#37)?我想我错过了实现这一点的路由和控制器方法 路线 API控制器 APIController: module Api module V1 class ApiController < ApplicationControlle

我的Rails应用程序中有两个API控制器用于RESTful设置:

  • StoresController(有许多产品)
  • ProductsController(有一个存储)
我如何编写API以便

仅返回该门店的产品(在本例中为门店#37)?我想我错过了实现这一点的路由和控制器方法

路线 API控制器 APIController:

    module Api
      module V1
        class ApiController < ApplicationController
          respond_to :json
          before_filter :restrict_access

          private

          def restrict_access
            api_app = ApiApp.find_by_access_token(params[:access_token])
            head :unauthorized unless api_app
          end
        end
      end
    end
  module Api
    module V1
      class StoresController < ApiController

        def index
          respond_with Store.all
        end

        def show
          respond_with Store.find_by_id(params[:id])
        end
      end
    end
  end
    module Api
      module V1
        class ProductsController < ApiController
          def index
            respond_with Product.all
          end

          def show
            respond_with Product.find_by_id(params[:id])
          end
        end
      end
    end
模块Api
模块V1
类ApiController<应用程序控制器
回复:json
前\u筛选器:限制\u访问
私有的
def限制用户访问
api\u app=ApiApp.find\u by\u access\u token(参数[:access\u token])
标题:未经授权,除非api_应用程序
结束
结束
结束
结束
StoresController:

    module Api
      module V1
        class ApiController < ApplicationController
          respond_to :json
          before_filter :restrict_access

          private

          def restrict_access
            api_app = ApiApp.find_by_access_token(params[:access_token])
            head :unauthorized unless api_app
          end
        end
      end
    end
  module Api
    module V1
      class StoresController < ApiController

        def index
          respond_with Store.all
        end

        def show
          respond_with Store.find_by_id(params[:id])
        end
      end
    end
  end
    module Api
      module V1
        class ProductsController < ApiController
          def index
            respond_with Product.all
          end

          def show
            respond_with Product.find_by_id(params[:id])
          end
        end
      end
    end
模块Api
模块V1
类存储控制器
产品控制器:

    module Api
      module V1
        class ApiController < ApplicationController
          respond_to :json
          before_filter :restrict_access

          private

          def restrict_access
            api_app = ApiApp.find_by_access_token(params[:access_token])
            head :unauthorized unless api_app
          end
        end
      end
    end
  module Api
    module V1
      class StoresController < ApiController

        def index
          respond_with Store.all
        end

        def show
          respond_with Store.find_by_id(params[:id])
        end
      end
    end
  end
    module Api
      module V1
        class ProductsController < ApiController
          def index
            respond_with Product.all
          end

          def show
            respond_with Product.find_by_id(params[:id])
          end
        end
      end
    end
模块Api
模块V1
类ProductsController

谢谢你的洞察力

您可以根据门店id确定产品范围

class ProductsController < ApiController
  def index
    store = Store.find(params[:store_id])
    respond_with store.products
  end
end

您会发现37是参数中提供的路由的一部分,可能在
:store_id
中。检查
rake路由
以确保。

您希望在路由中嵌套资源:

resources :stores do
  resources :products
end
所以你有这些路线:

GET        /stores/:id
GET/POST   /stores/:store_id/products
PUT/DELETE /stores/:store_id/products/:id
GET        /stores/:id
GET/POST   /stores/:store_id/products
PUT/DELETE /products/:id
您可能还需要浅路由,以避免深度嵌套的资源:

resources :stores, shallow:true do
  resources :products
end
所以你有这些路线:

GET        /stores/:id
GET/POST   /stores/:store_id/products
PUT/DELETE /stores/:store_id/products/:id
GET        /stores/:id
GET/POST   /stores/:store_id/products
PUT/DELETE /products/:id
获得路线后,您可以先加载父商店,然后使用产品关联:

@store = Store.find(params[:store_id])
@products = store.products