0
votes

A POST request shall create the resource and return a "Location" header pointing to the created resource.

I'm using grape API with rails and asking myself what is the best way to return the created resource URI. Is grape offering a way to get a resource URI without setting it manually ?

resource :blup do
  post '/' do
    blup = Blup.create(first_name: 'Jamie')
    if blup.present?
      header 'Location', 'HERE blup URI'
    end
  end
end
2
Try Rails.application.routes.url_helpers.blup_url blup.id - chumakoff

2 Answers

0
votes

For now I have not found anything better than this :

header 'Location', "#{request.url}/#{blup.id}"

But only work when request.url is the ressource root URI. (eg in a POST request)

0
votes

Though I do not know what Grape adds to Ruby on Rails API, I had the same problem, and I found the answer here. To give the location in a header after creation, you can add :

render location: specific_thing_i_am_looking_for_url(@thing)

And you can find specific_thing_i_am_looking_for_url by running

rake routes