pass block to Rails partial
Rails
ActionView
tested in 2024
in app/application/menu.html.erb
in app/application/menu.html.erb
<%= render "menu_component", local_assigns do %> <div class="py-1" role="none"> <a href="#" class="text-gray-700 group w-full flex items-center px-4 py-2 text-sm hover:text-grey-900 hover:bg-gray-100" role="menuitem"> Edit </a> </div> <% end %>
in app/application/menu_component.html.erb
<div class="relative block" data-controller="dropdown"> <button class="border-gray-200 rounded-full p-1 opacity-80 " data-dropdown-target="button " data-action="click->dropdown#toggle click@window->dropdown#hide"> <span>Open options</span> </button> <div data-dropdown-target="menu" class="hidden z-50 bg-white rounded-lg w-48 shadow-lg absolute right-0"> <%= yield %> </div> </div>
in any view just:
<%= render "menu" %>
-----------------------------------------------------------------------------------------------
OLD
Rails partials can accept blocks given you render them with "layout" option
<%= render layout: 'heading', locals: { title: "My Blog" } do %> <button>Logout</button> <% end %>
# a/v/application/_heading.html.erb <header class="flex justify-between"> <h1><%= title %></h1> <div class"right-side"> <%= yield %> </div> </header>