Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `:location => ...` and `head :ok` mean in the 'respond_to' format statement?

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 
like image 284
user502052 Avatar asked Mar 06 '11 22:03

user502052


People also ask

What is mean by head ok?

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.

What is Respond_to do format?

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.

What does Respond_to do in Ruby?

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.


1 Answers

  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.

like image 63
hoha Avatar answered Oct 04 '22 13:10

hoha