TIP: Rendering partials and collections
1. Using shorthand syntax: #
This:
<% @inbox.messages.each do |message| %>
<%= render partial: 'messages/message', locals: { message: message } %>
<% end %>
Equals this:
<%= render @inbox.messages %>
2. Less magic = More control! #
<%= render @messages %>
<%= render partial: "messages/message", collection: @messages, locals: { a: "b" } %>
<%= render @message %>
<%= render partial: "messages/message", locals: { message: @message, a: "b" } %>
3. Passing ONLY locals to a partial: #
- Does not work:
<%= render "layouts/foo", locals: { a: "b" } %>
- works:
<%= render partial: "layouts/foo", locals: { a: "b" } %>
- also works:
<%= render "layouts/foo", a: "b" %>
4. Check if a local variable is present #
local_assigns[:a].present?
# => true
local_assigns[:a].presence
# => "b"
local_assigns[:a]
# => "b"
Found this ^ one here
Resources:
Did you like this article? Did it save you some time?