rubyprince
rubyprince

Reputation: 17793

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?

Upvotes: 42

Views: 61141

Answers (3)

Babatunde Mustapha
Babatunde Mustapha

Reputation: 2653

 render :action => 'some_controller_action', :layout => 'some_layout_in_layout_folder'

will render the view of that controller action and apply the asset settings (javascript, CSS) of the some_layout_in_layout_folder layout.

If you write this in a controller action, it will use the styling of the specified layout in conjunction with any layout defined at the beginning of the class, if any

Upvotes: 0

christianblais
christianblais

Reputation: 2458

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'

Upvotes: 82

user3118220
user3118220

Reputation: 1468

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

Upvotes: 5

Related Questions