Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: What is the location option for in the render method

Hey I am wondering what the location option for the render method in rails is. The docs here http://guides.rubyonrails.org/layouts_and_rendering.html states:

"You can use the :location option to set the HTTP Location header:"

But I have no idea why you would do this, or what you would use this for.

like image 596
hajpoj Avatar asked Aug 23 '12 03:08

hajpoj


People also ask

How does render work in Rails?

Rails can render a raw file from an absolute path. This is useful for conditionally rendering static files like error pages. This renders the raw file (it doesn't support ERB or other handlers). By default it is rendered within the current layout.

How can you tell Rails to render without a layout?

By default, if you use the :text option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option.

What is render in Ruby?

Rendering is the ultimate goal of your Ruby on Rails application. You render a view, usually . html. erb files, which contain a mix of HMTL & Ruby code. A view is what the user sees.

What does render JSON mean?

render :json essentially calls to_json and returns the result to the browser with the correct headers. This is useful for AJAX calls in JavaScript where you want to return JavaScript objects to use. Additionally, you can use the callback option to specify the name of the callback you would like to call via JSONP.


1 Answers

Actually location option is used to redirect to a new resource as part of processing the request. For example,

 render :xml => post.to_xml, :status => :created, :location => post_url(post)

is telling the recipient that a XML file for the post is created and you will get this from post_url(post). Hence GO THERE ;)

render method does this by setting the Location option in response object

... ... ... 
if location = options[:location]
    response.headers["Location"] = url_for(location)
end
... ... ... 

You can find details about Location header here http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30.

like image 192
Samiron Avatar answered Nov 15 '22 13:11

Samiron