45

I am using Ruby on Rails 3 and I would like to know what the :location => ... and head :ok statements mean in following code, how they work and how I can\should use those.

respond_to do |format|
    format.xml  { render :xml => @user, :status => :created, :location => @user }
end

respond_to do |format|
    format.xml  { head :ok }
end
botbot
  • 7,299
  • 14
  • 58
  • 96
user502052
  • 14,803
  • 30
  • 109
  • 188

1 Answers1

76
  1. render ... :location => @user will set the HTTP location header to inform the client of the location of the newly created resource (that is, its URL)

  2. head :ok sets render to return an empty response (so just the header, no body) with status 200. head :ok is shorthand for render nothing: true, status: :ok.
    Here's a list of all the :status options you can use for setting the appropriate status code.

Dennis
  • 56,821
  • 26
  • 143
  • 139
hoha
  • 4,418
  • 17
  • 15
  • 4
    so what does the client do with the newly created user location? – highBandWidth Aug 07 '12 at 21:08
  • @highBandWidth Well whatever they want to do. Precise semantics of "location of a newly-created resource" depends on application. In the example above such URL can be used to show/edit user profile. – hoha Aug 13 '12 at 10:31
  • @hoha I am using url in location, along with head :ok, that needs a parameter. But i get the parameter's value as undefined. id = comment.assignment.id head :ok, location: assignment_lessons_path(id) Could you please help me here? Thanks. :) – Vishal Apr 17 '17 at 06:56