渲染 json 出现错误,未找到控制器渲染头的模板 no_content 204

rendering json gets error no template found for controller rendering head no_content 204

我有一个控制器可以使用来自模型的数据渲染 json。当我输入路由以获取它时,它什么也不做,只是在控制台中显示和错误。

控制器:


    class Api::ForecastController < ApplicationController
      before_action :set_hourly_forecast, only: %i[ show edit update destroy ]
    
    
      def index
        respond_to do |format|
            format.html do
              @hourly_forecasts = HourlyForecast.where(forecast_location_id: params[:forecast_location_id]).paginate(:page => params[:page], :per_page=>24) if params[:forecast_location_id].present?
            end
            format.json do
              weather_service = WeatherService.where(name: params[:name])
              @forecast_location = ForecastLocation.where(weather_service_id: weather_service)#& municipality: @municipality.name)
              @hourly_forecasts = HourlyForecast.where(forecast_location_id: forecast_location.id ).paginate(:page => params[:page], :per_page=>24) if params[:forecast_location_id].present?
            end
        end
      end
    
      private
        # Use callbacks to share common setup or constraints between actions.
        def set_hourly_forecast
          @hourly_forecast = HourlyForecast.find(params[:id])
        end
    
        # Only allow a list of trusted parameters through.
        def hourly_forecast_params
          params.require(:hourly_forecast).permit(:forecast_location_id, :date, :temperature, :humidity, :rain, :rain_probability, :wind_speed, :wind_direction)
        end
    end

错误:

> Started GET "/api/forecast.json?name=dark_sky" for 127.0.0.1 at 2022-04-20 18:33:29 +0200
Processing by Api::ForecastController#index as JSON
  Parameters: {"name"=>"dark_sky"}
No template found for Api::ForecastController#index, rendering head :no_content
Completed 204 No Content in 53ms (ActiveRecord: 6.2ms | Allocations: 4983)

我使用的路线

 127.0.0.1:3000/api/forecast.json?name=dark_sky

所以输出应该是一个 json 的所有小时模型。 但它什么都不做,在控制台上它确实得到了 select 但是跳过了模板的这个错误,我不明白它,因为我是 rails.

上的新手

如果需要更多控制器、模型或任何东西,请在评论中提问。

例如,您必须有一个单独的模板文件来呈现 json index.json.jbuilder

# app/views/api/forecasts/index.json.jbuilder

json.array! @hourly_forecasts do |hourly_forecast|
  json.extract! hourly_forecast, :id, :forecast_location_id, :date, :temperature, :humidity, :rain, :rain_probability, :wind_speed, :wind_direction
  json.url api_forecast_url(hourly_forecast, format: :json)
end

https://github.com/rails/jbuilder

如果您不需要过多自定义渲染 json,请在控制器中内联渲染 json

format.json do
  weather_service = WeatherService.where(name: params[:name])
  @forecast_location = ForecastLocation.where(weather_service_id: weather_service)#& municipality: @municipality.name)
  @hourly_forecasts = HourlyForecast.where(forecast_location_id: forecast_location.id ).paginate(:page => params[:page], :per_page=>24) if params[:forecast_location_id].present?

  render json: @hourly_forecasts
end

https://guides.rubyonrails.org/layouts_and_rendering.html#rendering-json

https://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html