Rails: Restrict API requests to JSON format

后端 未结 3 732
醉酒成梦
醉酒成梦 2021-02-03 23:31

I would like to restrict requests to all API controllers to being redirected to the JSON path. I would like to use a redirect since also the URL should change according to the r

3条回答
  •  长发绾君心
    2021-02-04 00:15

    Setting a default in your routes won't turn all requests into a JSON request.

    What you want is to make sure that whatever you're rendering is a JSON response

    You pretty much had it in the first option except you need to do this

    before_filter :set_default_response_format
    
    private
      def set_default_response_format
        request.format = :json
      end
    

    That would go under your Base API controller so that when it gets to your actual action the format will always be JSON.

提交回复
热议问题