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?
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.
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.
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.
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'
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
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