Weird Rails partials bug with View Components
Content rendered outside partials with View Components helpers

I'ma self-taught developer on my journey to become a successful indie dev.
For more content follow me on Twitter @adrianthedev
Search for a command to run...
Content rendered outside partials with View Components helpers

I'ma self-taught developer on my journey to become a successful indie dev.
For more content follow me on Twitter @adrianthedev
No comments yet. Be the first to comment.
I want to share my experience regarding AI-driven development.I know it's not a new and shiny thing, and it's been around for a while now, but some things should need some time to take it through its

I think it's important that the Ruby community knows what just happened in San Francisco last week. 400+ Rubyists got together and shared ideas and visions. They exchanged opinions and presented the past, present, and future of Ruby. I went to very ...

Tl;DR; We're taking a break with Friendly.rb. We had three beautiful editions, incredible fun, and learned so much in the process. This happening changed us, the hosts in ways we could not imagine. We're taking a break because things have happened in...

What do you need in your digital brain? That's how I think about Avo now. It's not some closeted admin panel where only five people dare to go because touching the data wrong might crash the app. It's the place where you invite your whole team to run...
Here's something that's been on my mind lately about the Rails ecosystem. For the past 10 years, there's been massive investment in dev tooling, especially for JavaScript and TypeScript companies. They take some of that funding and deploy it to marke...
Own it - A blog from Adrian Marin
24 posts
Sharing my lessons on becoming a thriving #indiedev.
๐ Launching Avo into the world! - ๐ฅ A beautiful, easy-to-use Ruby on Rails admin panel framework
On this page
Yesterday, during my regular Avo coding sessions I ran into a strange-looking bug.
I created a View Component names ResourceShow inside which I used a helper a_link that renders a button-looking link using a helper and a regular Rails partial.
The code looks like this:
# application_helper.rb
module ApplicationHelper
def a_link(label, url = nil, **args, &block)
args[:style] = 'border: 1px solid red;'
if block_given?
url = label
end
locals = {
label: label,
url: url,
args: args,
}
if block_given?
render layout: 'layouts/a_link', locals: locals do
capture(&block)
end
else
render partial: 'layouts/a_link', locals: locals
end
end
end
# application_helper.rb
<% if (block = yield).empty? %>
<%= link_to label, url, **args %>
<% else %>
<%= link_to url, **args do %>
<%= yield %>
<% end %>
<% end %>
So running this <%= a_link '/' do %>Content inside the button<% end %> should render a basic button.
Because we'll be using this helper inside a view component, by checking their docs on helpers we need to prepend the helpers. function, so we'll do <%= helpers.a_link '/' do %>Content inside the button<% end %>.
But instead of our plain button, we'll get this weird bug.

The content inside the block will get duplicated and prepended outside the whole button ๐คฏ
You just need to stop using the helpers prepend method in the template and include the helper module in the view component ๐ฅ.
# app/components/hello_component.rb
class HelloComponent < ViewComponent::Base
include ApplicationHelper
end
# app/components/hello_component.html.erb
<%= a_link '/' do %>
Content inside the button
<% end %>

I'm writing this hoping that this will help someone in the future not lose their minds with this weird partials bug.
Stay sane, stay positive โ๏ธ
Photo by Greta Pichetti
Source code on GitHub