Rails无法为nil:NilClass呈现JSON未定义的方法`new'

时间:2022-07-30 20:40:31

Background

I'm making a Rails application which has two namespaces, which are admin (for administrator purpose) and api for mobile & web client.

我正在创建一个Rails应用程序,它有两个命名空间,分别是admin(用于管理员目的)和api用于移动和Web客户端。

A weird thing happened yesterday. I made a new table facilities which consists of id, name, created_at, updated_at in my PostgreSQL database.

昨天发生了一件奇怪的事。我在PostgreSQL数据库中创建了一个新的表设施,包括id,name,created_at,updated_at。

Problem

I tried to get all facilities using admin namespace http://localhost:3000/admin/facilities and it works well (return the HTML and the list of facilities).

我试图使用管理命名空间http:// localhost:3000 / admin / facilities获得所有设施,并且它运行良好(返回HTML和设施列表)。

Started GET "/admin/facilities" for ::1 at 2015-05-10 16:12:47 +0800
Processing by Admin::FacilitiesController#index as HTML
  Facility Load (0.4ms)  SELECT "facilities".* FROM "facilities"
  Rendered admin/facilities/index.html.erb within layouts/application (2.5ms)
Completed 200 OK in 91ms (Views: 90.4ms | ActiveRecord: 0.4ms)

But when I call http://localhost:3000/api/v1/facilities from browser, which supposed to return JSON. I got an error

但是当我从浏览器调用http:// localhost:3000 / api / v1 / facilities时,它应该返回JSON。我收到了一个错误

NoMethodError in Api::V1::FacilitiesController#index undefined method `new' for nil:NilClass

Api中的NoMethodError :: V1 :: FacilitiesController #index未定义方法`new'for nil:NilClass

Extracted source (around line #8):
6  def index
7    @facilities = Facility.all
8    render json: @facilities
9  end
10
11

The error from console

来自控制台的错误

Started GET "/api/v1/facilities" for ::1 at 2015-05-10 16:38:26 +0800
Processing by Api::V1::FacilitiesController#index as HTML
  User Load (0.6ms)  SELECT  "users".* FROM "users" WHERE "users"."id" IS NULL  ORDER BY "users"."id" ASC LIMIT 1
  Facility Load (0.5ms)  SELECT "facilities".* FROM "facilities"
Completed 500 Internal Server Error in 6ms (ActiveRecord: 1.1ms)

NoMethodError (undefined method `new' for nil:NilClass):
  app/controllers/api/v1/facilities_controller.rb:8:in `index'


  Rendered /Users/abrahamks/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (7.1ms)
  Rendered /Users/abrahamks/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.7ms)
. . . .
. . . .
and so on

I check from rails c and call Facility.all and looks like the command line return correct values, but I don't understand why it can't render / return json.

我从rails c检查并调用Facility.all看起来像命令行返回正确的值,但我不明白为什么它不能渲染/返回json。

Loading development environment (Rails 4.2.1)
2.2.1 :001 > Facility.all
  Facility Load (0.5ms)  SELECT "facilities".* FROM "facilities"
 => #<ActiveRecord::Relation [#<Facility id: 1, name: "Kitchen", created_at: "2015-05-09 10:54:00", updated_at: "2015-05-09 16:08:48">, #<Facility id: 2, name: "Washer", created_at: "2015-05-09 11:20:40", updated_at: "2015-05-09 16:09:32">, #<Facility id: 3, name: "Swimming Pool", created_at: "2015-05-09 11:22:19", updated_at: "2015-05-09 16:09:41">, #<Facility id: 4, name: "Internet", created_at: "2015-05-09 11:24:02", updated_at: "2015-05-09 16:12:31">, #<Facility id: 5, name: "Dryer", created_at: "2015-05-09 15:55:36", updated_at: "2015-05-09 16:12:54">]> 

What's wrong with my code? Is there any configuration that I missed? Thank you.

我的代码出了什么问题?我错过了任何配置吗?谢谢。




More Details if Needed

# GET /facilities/1 facilities has many-to-many relationship with properties, I generate a join table

#GET / facilities / 1设施与属性有多对多的关系,我生成一个连接表

class CreateJoinTablePropertiesFacilities < ActiveRecord::Migration
  def change
    create_join_table :properties, :facilities do |t|
      t.index [:property_id, :facility_id]
      t.index [:facility_id, :property_id]
    end
  end
end

Here is my facility.rb

这是我的facility.rb

class Facility < ActiveRecord::Base
    has_and_belongs_to_many :property, join_table: :facilites_properties
end

Here is my routes.rb

这是我的routes.rb

Rails.application.routes.draw do
  namespace :admin do
    resources :facilities
  end

  namespace :api do
    namespace :v1 do
      resources :properties
      resources :facilities, only: [:index, :show]
      resources :users do
        member do
        post 'list', :to => "users#list_property"
    end
  end

Here is my app/controllers/api/v1/facilities_controller

这是我的app / controllers / api / v1 / facilities_controller

class Api::V1::FacilitiesController < ApplicationController
  before_action :set_facility, only: [:show, :edit, :update, :destroy]
  # before_action :authenticate
  # GET /facilities
  # GET /facilities.json
  def index
    @facilities = Facility.all
    render json: @facilities
  end

  # GET /facilities/1
  # GET /facilities/1.json
  def show
    render json: @facility
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_facility
      @facility = Facility.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def facility_params
      params.require(:facility).permit(:name)
    end
end

And this is app/controllers/admin/facilities_controller

这是app / controllers / admin / facilities_controller

class Admin::FacilitiesController < ApplicationController
  before_action :set_facility, only: [:show, :edit, :update, :destroy]
  # before_action :authenticate
  # GET /facilities
  # GET /facilities.json
  def index
    @facilities = Facility.all
  end

  # GET /facilities/1
  # GET /facilities/1.json
  def show
  end

  # GET /facilities/new
  def new
    @facility = Facility.new
  end

  # GET /facilities/1/edit
  def edit
  end

  # POST /facilities
  # POST /facilities.json
  def create
    @facility = Facility.new(facility_params)

    respond_to do |format|
      if @facility.save
        format.html { redirect_to admin_facilities_path, notice: 'Facility was successfully created.' }
        format.json { render :show, status: :created, location: @facility }
      else
        format.html { render :new }
        format.json { render json: @facility.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /facilities/1
  # PATCH/PUT /facilities/1.json
  def update
    respond_to do |format|
      if @facility.update(facility_params)
        format.html { redirect_to admin_facilities_path, notice: 'Facility was successfully updated.' }
        format.json { render :show, status: :ok, location: @facility }
      else
        format.html { render :edit }
        format.json { render json: @facility.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /facilities/1
  # DELETE /facilities/1.json
  def destroy
    @facility.destroy
    respond_to do |format|
      format.html { redirect_to facilities_url, notice: 'Facility was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_facility
      @facility = Facility.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def facility_params
      params.require(:facility).permit(:name)
    end
end

2 个解决方案

#1


The stack trace is showing the request was processed as HTML.

堆栈跟踪显示请求已作为HTML处理。

Processing by Admin::FacilitiesController#index as HTML

As you are trying to render JSON, you can either specify the controller to respond to json

当您尝试渲染JSON时,您可以指定控制器来响应json

module Api
  module V1
    class FacilitiesController < ApplicationController
      respond_to :json
    end
  end
end

Or you can specify json as a default response within your routes.rb file

或者,您可以在routes.rb文件中将json指定为默认响应

namespace :api, defaults: { format: 'json' }  do
  # your api json routes...
end

Hope either one of these help.

希望其中任何一个有所帮助。

#2


I just realised I forgot to generate the serializer. Since I use gem Active Model Serializers, this is what I did to generate the serializer.

我刚刚意识到我忘了生成序列化器。由于我使用gem Active Model Serializers,这就是我用来生成序列化器的方法。

rails g serializer facility

rails g serializer facility

#1


The stack trace is showing the request was processed as HTML.

堆栈跟踪显示请求已作为HTML处理。

Processing by Admin::FacilitiesController#index as HTML

As you are trying to render JSON, you can either specify the controller to respond to json

当您尝试渲染JSON时,您可以指定控制器来响应json

module Api
  module V1
    class FacilitiesController < ApplicationController
      respond_to :json
    end
  end
end

Or you can specify json as a default response within your routes.rb file

或者,您可以在routes.rb文件中将json指定为默认响应

namespace :api, defaults: { format: 'json' }  do
  # your api json routes...
end

Hope either one of these help.

希望其中任何一个有所帮助。

#2


I just realised I forgot to generate the serializer. Since I use gem Active Model Serializers, this is what I did to generate the serializer.

我刚刚意识到我忘了生成序列化器。由于我使用gem Active Model Serializers,这就是我用来生成序列化器的方法。

rails g serializer facility

rails g serializer facility