Heroku只在我的api应用程序上的三个操作中的一个操作上给出了内部服务器错误



我已经构建了一个api,它在被调用时提供3个不同的数据集合。每个系列都是不同的类别,即:餐厅、海滩餐厅和咖啡馆。

以下是我的路线:

Rails.application.routes.draw do
devise_for :users
namespace :api, defaults: { format: :json } do
namespace :v1 do
resources :ads, only: [ :index, :show ] do
collection do
get 'restaurant', to: 'ads#restaurant'
get 'beach_restaurant', to: 'ads#beach_restaurant'
get 'cafe', to: 'ads#cafe'
end
end
end
end
root to: 'pages#home'

以下是我在api控制器中所做的操作:

class Api::V1::AdsController < Api::V1::BaseController
before_action :set_ad, only: [ :show ]
def index
@ads = policy_scope(Subcategory.find_by(nombre: "Restaurantes").ads)
end
def restaurant
@restaurants = policy_scope(Subcategory.find_by(nombre: "Restaurantes").ads)
end
def beach_restaurant
@beach_restaurants = policy_scope(Subcategory.find_by(nombre: "Restaurantes de playa").ads)
end
def cafe
@cafes = policy_scope(Subcategory.find_by(nombre: "Cafés y Heladerías").ads)
end
def show
end
private
def set_ad
@ad = Ad.find(params[:id])
authorize @ad
end
end

这是在我的BaseController中:

class Api::V1::BaseController < ActionController::API
include Pundit
after_action :verify_authorized, except: [:index, :restaurant, :beach_restaurant, :cafe]
after_action :verify_policy_scoped, only: [:index, :restaurant, :beach_restaurant, :cafe]
rescue_from StandardError,                with: :internal_server_error
rescue_from Pundit::NotAuthorizedError,   with: :user_not_authorized
rescue_from ActiveRecord::RecordNotFound, with: :not_found
private
def user_not_authorized(exception)
render json: {
error: "Unauthorized #{exception.policy.class.to_s.underscore.camelize}.#{exception.query}"
}, status: :unauthorized
end
def not_found(exception)
render json: { error: exception.message }, status: :not_found
end
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
end

然后我提出了三个不同的观点:restaurant.json.jbuilder、beach_restaurant.json.jbuilder和cafe.json.jbbuilder

restaurant.json.jbuilder

json.array! @restaurants do |restaurant|
json.extract! restaurant,
:id,
:empresa,
:direccion_principal,
:tel,
:email_principal,
:web,
:facebook,
:instagram,
:twitter,
:isla
end

beach_restaurant.json.jbuilder

json.array! @beach_restaurants do |beach_restaurant|
json.extract! beach_restaurant,
:id,
:empresa,
:direccion_principal,
:tel,
:email_principal,
:web,
:facebook,
:instagram,
:twitter,
:isla
end

cafe.json.jbuilder

json.array! @cafes do |cafe|
json.extract! cafe,
:id,
:empresa,
:direccion_principal,
:tel,
:email_principal,
:web,
:facebook,
:instagram,
:twitter,
:isla
end

当我用Postman在本地服务器上请求这3个操作时,它们运行良好,但一旦部署在Heroku上,cafe操作就会产生内部服务器错误(状态:500(。我看不出这是怎么可能的,因为咖啡馆的设置方式和餐厅和海滩餐厅的设置方式一样,而这两个动作都是在heroku上进行的。有人知道为什么会这样吗?

这就是heroku logs --tail显示的内容:

2018-12-04T06:19:38.704997+00:00 heroku[router]: at=info method=GET path="/api/v1/ads/cafe" host=my-app.herokuapp.com request_id=97694e3f-91fe-41fc-b2fc-de96d41d134d fwd="83.54.89.18" dyno=web.1 connect=0ms service=470ms status=500 bytes=274 protocol=https
2018-12-04T06:19:38.578597+00:00 app[web.1]: D, [2018-12-04T06:19:38.578458 #4] DEBUG -- : [97694e3f-91fe-41fc-b2fc-de96d41d134d]    (41.7ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
2018-12-04T06:19:38.703397+00:00 app[web.1]: D, [2018-12-04T06:19:38.701104 #4] DEBUG -- : [97694e3f-91fe-41fc-b2fc-de96d41d134d]   Subcategory Load (39.7ms)  SELECT  `subcategorias`.* FROM `subcategorias` WHERE `subcategorias`.`nombre` = 'Cafés y Heladerías' LIMIT 1
2018-12-04T06:19:38.703401+00:00 app[web.1]: I, [2018-12-04T06:19:38.702341 #4]  INFO -- : [97694e3f-91fe-41fc-b2fc-de96d41d134d] Completed 500 Internal Server Error in 462ms (Views: 0.4ms | ActiveRecord: 121.7ms)

我通过在控制器中使用.find((而不是.find_by((解决了这个问题。我想在Heroku上查找类别的字符串名称时会失败。在这种情况下,按id查找类别更好。

所以正确的方法是:

def beach_restaurant
@cafes = policy_scope(Subcategory.find(3).ads)
end

相关内容

最新更新