Where can I find and how to modify generated html form elements in rails7

49 Views Asked by At

i have this code in rails <%= button_to "Destroy this post", @post, method: :delete %> and it is returning <button .... /button> but I want to return <bx-btn .... /bx-btn> instead. How can I achieve that and where exactly I can modify the code? Thank you!

I tried to search but nothing results in sufficient explanatory to mi issue or I could not comprehend the answer. I tried rails guides but I cant seem to find the documentation or I cant decribe what I am searching for.

1

There are 1 best solutions below

5
On

If what you want is to monkey-patch button_to, probably isn't a good idea.
A better choice might be to create a helper that works as a content_tag extension, like:

def bx_button(content_or_options_with_block = nil, options = nil, escape = true, &block)
  content_tag('bx-btn', content_or_options_with_block, options, escape) do
    block.call if block_given?
  end
end

then use it as:

<%= bx_button "Destroy this post", method: :delete %>

or as a block:

<%= bx_button method: :delete do %>
  Destroy this post
  <any-other-tag-you-need-inside-bx-btn/>
<% end %>