【问题标题】:Where can I find and how to modify generated html form elements in rails7我在哪里可以找到以及如何修改 rails7 中生成的 html 表单元素
【发布时间】:2022-11-26 12:04:55
【问题描述】:

我在 rails 中有这段代码 <%= button_to "Destroy this post", @post, method: :delete %> 它正在返回<button .... /button>,但我想返回<bx-btn .... /bx-btn>。我怎样才能做到这一点以及我可以在哪里修改代码?谢谢!

我试图搜索,但没有任何结果足以解释我的问题,或者我无法理解答案。我试过 Rails 指南,但我似乎找不到文档,或者我无法描述我正在搜索的内容。

【问题讨论】:

    标签: web-component ruby-on-rails-7 carbon-design-system


    【解决方案1】:

    如果你想要的是猴子补丁button_to,可能不是一个好主意。
    更好的选择可能是创建一个作为 content_tag 扩展名的助手,例如:

    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
    

    然后将其用作:

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

    或者作为一个块:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-17
      • 1970-01-01
      • 2017-04-13
      • 2022-12-24
      • 2011-01-20
      • 2011-10-31
      • 2014-02-22
      相关资源
      最近更新 更多