Undefined instance method “respond_to” in Rails 5 API Controller

前端 未结 2 1784
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-13 05:51

In rails 5 created with --api I have an error

NoMethodError (undefined method `respond_to\' for #
Did         


        
2条回答
  •  温柔的废话
    2020-12-13 06:26

    ActionController::API does not include the ActionController::MimeResponds module. If you want to use respond_to you need to include MimeResponds.

    class ApplicationController < ActionController::API
      include ActionController::MimeResponds
    end
    
    
    class Api::MyController < ApplicationController
      def method1
        # ...
        respond_to do |format|
          format.xml { render(xml: "fdsfds") }
          format.json { render(json: "fdsfdsfd" ) }
        end
      end
    end
    

    Source: ActionController::API docs

提交回复
热议问题