Passing Variables to View Partials
30 Dec 2022Sometimes the multiple ways that variables are passed to partials confuses me.
locals
Using locals
, whenever you render a partial you must declare the same variable. If you attempt to render the same partial in a different view template without declaring the same local variable, you’ll get an error.
<%= render :partial => 'form', :locals => { :post => @post } %>
local_assigns
In cases where sometimes you want to pass a variable to a partial but other times you don’t, use local_assigns
. Be aware that you will need to check for the existense of a variable in the partial before you use it.
<%= render article, full: true %>
<!-- in template -->
<h2><%= article.title %></h2>
<% if local_assigns[:full] %>
<%= simple_format article.body %>
<% else %>
<%= truncate article.body %>
<% end %>
object
Every partial also has a local variable with the same name as the partial (minus the leading underscore). You can pass an object in to this local variable via the :object
option:
<%= render partial: "customer", object: @new_customer %>
If you see the super shorthand syntax:
<%= render @customer %>
it uses object: @customer
.
Local Variables in Collections
These work essentially the same as with single object view partials. To customize the name of the local variable, use the :as
option:
<%= render partial: "product", collection: @products, as: :item %>
You can also still create local variables using locals
like before:
<%= render partial: "product", collection: @products,
as: :item, locals: {title: "Products Page"} %>
Read more in the Rails Guide.