Rails respond_with — why does POST return a URL instead of the data?

前端 未结 3 617
甜味超标
甜味超标 2021-02-12 14:57

This is a question \"why does it work this way\", not \"how do I make this work\".

My app is calling a third party REST API that returns JSON, and returning the result a

3条回答
  •  忘掉有多难
    2021-02-12 15:44

    The 'why' has been answered excellently by @david-james. This is just a short 'how' to answer via respond_with:

    class Api::V1::UsersController < ApplicationController
    
      respond_to :json
    
      def create
        @user = User.create(...)
        respond_with @user, location: url_for([:api, :v1, @user])
      end
    
    end
    

提交回复
热议问题