Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between render :action and render :template

What is the difference between render :action => "new" and render :template => "users/new"? I have heard that rendering template, we can use for views from other controllers. Is that it or is there any difference in rendering layout also between the two? For render :template, is it neccessary to have an action defined or is the view page itself enough?

like image 258
rubyprince Avatar asked Feb 18 '11 18:02

rubyprince


People also ask

What does it mean to render a template?

Rendering a template is indeed always about producing content, but for a slightly wider description of content. It could be a chunk of html, for example an ajax call to get new items might produce some html describing the new items, but it doesn't have to be.

What is render in rails?

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 do in Rails?

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.


2 Answers

There is no difference.
render :template => 'some/thing' is the same as just render 'some/thing', as well as the same as render :action => 'thing' if we are in the some controller.

From Ruby On Rails guide;

render :edit render :action => :edit render 'edit' render 'edit.html.erb' render :action => 'edit' render :action => 'edit.html.erb' render 'books/edit' render 'books/edit.html.erb' render :template => 'books/edit' render :template => 'books/edit.html.erb' render '/path/to/rails/app/views/books/edit' render '/path/to/rails/app/views/books/edit.html.erb' render :file => '/path/to/rails/app/views/books/edit' render :file => '/path/to/rails/app/views/books/edit.html.erb' 
like image 55
christianblais Avatar answered Sep 20 '22 01:09

christianblais


Previously, calling render "foo/bar" in a controller action was equivalent to render file: "foo/bar". In Rails 4.2, this has been changed to mean render template: "foo/bar" instead. If you need to render a file, please change your code to use the explicit form (render file: "foo/bar") instead.

http://guides.rubyonrails.org/4_2_release_notes.html#render-with-a-string-argument

like image 30
user3118220 Avatar answered Sep 21 '22 01:09

user3118220