如何将查询参数传递给Rails API控制器?

问题描述 投票:0回答:2

在我的Ruby on Rails应用程序中,自行车租赁公司可以管理其所有自行车(预订,付款等)。

上下文我想为自行车租赁公司(shops)提供在其自己的网站上执行预订表格的选项,以便他们可以让客户预订bike

  • 然后此预订表格将显示bike_categories,其中bikes在给定的arrivaldeparture日期可用。

问题为了对此进行管理,我想生成一个API控制器动作,该动作显示某个availabilitybike_category,并显示属于该count的可用bikes的数量的bike_category

据此帖子

Design RESTful query API with a long list of query parameters

我应该能够在api中处理查询,但是如何在Rails控制器中获取查询?

代码

模型

class Shop < ApplicationRecord
  has_many :bike_categories, dependent: :destroy
  has_many :bikes, through: :bike_categories
  has_many :reservations, dependent: :destroy
end

class Reservation < ApplicationRecord
  belongs_to :shop
  belongs_to :bike
end

class Bike < ApplicationRecord
  belongs_to :bike_category
  has_many :reservations, dependent: :destroy
end

class BikeCategory < ApplicationRecord
  belongs_to :shop
  has_many :bikes, dependent: :destroy
end

路线

# api
  namespace :api, defaults: { format: :json } do
    namespace :v1 do
      resources :shops, only: [ :show ]
      resources :reservations, only: [ :show, :create ]
      resources :bike_categories, only: [:index, :show, :availability]
    end
  end

controller / api / v1 / bike_categories_controller.rb


class Api::V1::BikeCategoriesController < Api::V1::BaseController
  acts_as_token_authentication_handler_for User, only: [:show, :index, availability]

  def availability
    # How to get the bike_category, arrival and departure?
  end

end
ruby-on-rails json api-design
2个回答
1
投票

Rails在params哈希中提供查询字符串参数,它实际上是以下项的混搭:

  • 请求主体(用于具有主体的POST,PATCH,PUT等方法)
  • 查询字符串
  • 来自路径的命名段(例如params[:id])。

所以实际上什么也没做:

class Api::V1::BikeCategoriesController < Api::V1::BaseController
  acts_as_token_authentication_handler_for User, only: [:show, :index, availability]
  def availability
    # How to get the bike_category, arrival and departure?
    bike_category = params[:bike_categories] 
    arrival = params[:arrival]
    departure = params[:departure]
  end
end

但是您的路由配置不正确。您实际上没有availiblity的路线。 onlyexcept选项仅限制resources生成的默认CRUD路由的输出。因此,有效值为:

  • 显示
  • 索引
  • 创建
  • 编辑
  • 更新
  • 删除

其他任何值都完全无效。如果要添加其他RESTful路由,可以这样:

resources :bike_categories, only: [:index, :show] do
  # GET .../bike_categories/:bike_category_id/availability 
  get :availability 

  # GET .../bike_categories/:id/availability 
  get :availability, on: :member

  # GET .../bike_categories/availability 
  get :availability, on: :collection
end

0
投票

您可以使用has_scope gem

在Bike模型中,您可以创建范围not_reserved(假设您将到达和离开的列命名为arrive_atdeparture_at

class Bike < ApplicationRecord
# ...
  scope :not_reserved, -> arrive, departure { left_outer_joins(:reservations).distinct.where("reservations.arrival_at < ? AND reservations.departure_at > ?", departure, arrive) }
  # If you need to return the result for a specific bike category
  scope :by_bike_category, -> bike_category_id { where(bike_category_id: bike_category_id) }
# ...
end

在您的BikeCategoriesController中:

class Api::V1::BikeCategoriesController < Api::V1::BaseController
  has_scope :not_reserved, using: [:arrive, :departure], type: :hash
  has_scope :by_bike_category

  def availability
    category_availability = apply_scopes(Bike).group(:bike_category_id).count

    render json: category_availability
  end

end

您的查询字符串将类似于:

?not_reserved[arrive]=20200110&not_reserved[departure]=20200125&by_bike_category=3
© www.soinside.com 2019 - 2024. All rights reserved.