ruby-on-rails Rails星号/"*”未出现在表单的一个标签中

cigdeys3  于 4个月前  发布在  Ruby
关注(0)|答案(2)|浏览(65)

情况

有一个简单的形式,其中抽象的部分,我张贴在下面:

<div class="form-group">
    <%= f.label :net %>
    <div class="input-group  w-50">
      <%= f.text_field :net,
        required: true,
        class: 'form-control text-right' %>
      <div class="input-group-append">
        <span class="input-group-text" style="width: 40px;">€</span>
      </div>
    </div>
  </div>

字符串
&

<div class="form-group">
            <%= f.label :vat_rate, t('in_invoice.form.vat_rate') %>
            <%= render 'home/shared/standard_vat'  %>
            <div class="input-group w-50">
              <%= f.text_field :vat_rate,
                               required: true,
                               class: 'form-control text-right' %>
              <div class="input-group-append">
                <span class="input-group-text" style="width: 40px;">%</span>
              </div>
            </div>
          </div>


型号:

...

  validates :net, :number, :vat_rate, presence: true
  validates :net, numericality: { greater_than: 0}
  validates :vat_rate, numericality: { only_integer: true, greater_than_or_equal_to: 0}
  validates :net, numericality: true

    ...

我的问题我想在vat_rate上放一个星号(截图:“VAT-Rate in %”)标签。但由于某种原因,它只出现在:net:number字段上,尽管它们相等(例如去除其间的部分,includind presence validation),正如您在屏幕截图x1c 0d1x上看到的那样。(我认为这无关紧要,但表单是用simple_form_for初始化的simple_form

ippsafx7

ippsafx71#

星号是通过简单的形式添加的。当你传递文本作为第二个参数时,你会覆盖所有内容。如果你想保留*传递文本作为一个选项:

<%= f.label :vat_rate, label: t('in_invoice.form.vat_rate') %>
#                      ^^^^^^

字符串

  • 网址:http://github.com/heartcombo/simple_form/blob/v5.3.0/lib/simple_form/form_builder.rb#L319*

如果你的simple_form看起来不像这样,你就错过了简单形式的要点:

<%= simple_form_for @invoice do |f| %>
  <%= f.input :net %>
  <%= f.input :vat_rate %>
<% end %>


您应该只配置一次输入,只需使用input helper:

  • 网址:http://github.com/heartcombo/simple_form/tree/main#configuration*

举例来说:

# config/initializers/simple_form.rb

module InputGroup
  def append(wrapper_options = nil)
    template.content_tag(:span, options[:append], class: "input-group-text")
  end
end
SimpleForm.include_component(InputGroup)

SimpleForm.setup do |config|
  # https://github.com/heartcombo/simple_form-bootstrap
  config.wrappers :group, tag: "div", class: "form-group", error_class: "form-group-invalid", valid_class: "form-group-valid" do |b|
    b.use :html5
    b.use :placeholder
    b.optional :maxlength
    b.optional :minlength
    b.optional :pattern
    b.optional :min_max
    b.optional :readonly
    b.use :label

    b.wrapper :input_group_tag, tag: "div", class: "input-group-append" do |ba|
      ba.use :input, class: "form-control", error_class: "is-invalid", valid_class: "is-valid"
      ba.optional :append
    end

    b.use :full_error, wrap_with: {tag: "div", class: "invalid-feedback d-block"}
    b.use :hint, wrap_with: {tag: "small", class: "form-text text-muted"}
  end
end
<%= simple_form_for @invoice do |f| %>
  <%= f.input :net,      wrapper: :group, append: "€" %>
  <%= f.input :vat_rate, wrapper: :group, append: "%" %>
<% end %>

的字符串

nbewdwxp

nbewdwxp2#

总结一下:“要求:true/false[DEFAULT]”是标签标签的一个选项,我通过设置标签覆盖了它。删除了标签,现在它可以工作了。设置它也可以工作,但需要更少的代码:-)

相关问题