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
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.
respond_to is a method on the superclass ActionController . it takes a block, which is like a delegate. The block is from do until end , with |format| as an argument to the block. respond_to executes your block, passing a Responder into the format argument.
A respond_to shortcut it works the same way as writing the full respond_to block in index . It's a short way to tell Rails about all the formats your action knows about. And if different actions support different formats, this is a good way to handle those differences without much code.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With