Files
national-parks-api/app/controllers/api/v1/parks_controller.rb

25 lines
520 B
Ruby

module Api::V1
class ParksController < BaseController
before_action :set_park, only: :show
def index
parks = Park.all
if params[:state].present?
parks = parks.where("states like '%' || ? || '%'", params[:state])
end
render json: parks.limit(per_page).offset((page - 1) * per_page)
end
def show
render json: @park
end
private
def set_park
@park = Park.find_by(code: params[:code])
head :not_found unless @park.present?
end
end
end