Rails: Restrict API requests to JSON format

后端 未结 3 736
醉酒成梦
醉酒成梦 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:21

    Second option, using routes format. If a user explicitly requests a XML format, he should not receive a JSON response. He should get a message saying that this URL doesn't answer to XML format, or 404.

    By the way, it would be rather easy to respond to all which is what you should do in my opinion.

    class FooController
      respond_to :xml, :json
      def show
        @bar = Bar.find(params[:id])
        respond_with(@bar)
      end
    end
    

提交回复
热议问题