Posted by
flyerhzm
on
December 04, 2010
render is one of the often used view helpers, we can pass object, collection or local variables. From rails 2.3, more simplified syntax for render are provided.
render is one of the often used view helpers, we use it to extract sub part view. We can pass object, collection or local variables to the partial views. From rails 2.3, more simplified syntax for render are provided that makes render helper cleaner. Here I will show you the contrast.
Render simple partial
Before
<%= render :partial => 'sidebar' %>
<%= render :partial => 'shared/sidebar' %>
After
<%= render 'sidebar' %>
<%= render 'shared/sidebar' %>
Render partial with object
Before
<%= render :partial => 'posts/post', :object => @post %>
After
<%= render @post %>
Render partial with collection
Before
<%= render :partial => 'post', :collection => @posts %>
After
<%= render @posts %>
Render partial with local variables
Before
<%= render :partial => 'comments/comment', :locals => { :parent => post } %>
After
<%= render 'comments/comment', :parent => post %>

Comments
in rails 3.0.1, i test below hints not works.
Render partial with local variables
Before
<%= render :partial => 'comments/_comment', :locals => { :parent => post } %>
After
<%= render 'comments/_comment', :parent => post %>
but this is good tips also.thanks for your share.:-)
but the syntax need be careful use
<%= render :partial => 'posts', :collection => @posts %>
Read
<%= render :partial => 'post', :collection => @posts %>
Note the singular and plural form.
<%= render :partial => 'some/path/to/user', :collection => @users %>
to
<%= render 'some/path/to/user', :collection => @users %>
i dont have access to user_counter within the partial anymore. rest works fine.
= render :partial => "lists/contacts", :locals => {:show=> false, :long_header => true}
render @experts, :locals => {:category => @category}
gives
Missing partial admin/admin/experts/expert (note admin/admin - I use namespaces in my app and folders accordingly)
http://stackoverflow.com/questions/4998448/specify-namespace-when-using-render-with-an-object-in-rails-3
Is there a way to send extra local variable along with collection
Basically all i want is to do
<%= render @posts, :article => @article %>
as my "posts/_post.html.erb" file has article variable too and i want to pass it along with collections of@post
Thanks
= render :partial => "shared/bin", :collection => @bins, :locals => { :task => @task }
If I simplyfy it to
= render "shared/bin", collection: @bins, task: @task
then it doesn't work. Even if I just leave
= render "shared/bin", collection: @bins
for testing the same error appears -- var bin is nil in partial
:partial must be used in the case of using a :collection and :as parameters.