Rails 中的 Bootstrap + Simple Form 验证失败,但字段没有变成红色

问题描述 投票:0回答:1

我研究了一下,还有另一个具有相同标题的问题,但解决方案没有帮助/适用。

问题:尽管验证在保存对象时失败 - 在本例中是更新,但 simple_form 中的字段不会变成红色。我在这里测试了 6 个字段,更新验证失败。但没有出现任何标记。我习惯于在验证失败时看到红色字段。

我正在发布我的编辑视图来帮助解决问题。

edit.html.erb

<div class="row">
  <div class="col-8 offset-2">
    <%= link_to t('common.buttons.back'), home_company_path(id: current_company) %>
    <%= simple_form_for @company, url: home_company_path do |f| %>
      <div class="row">
        <div class="w-100">
          <div class="ibox float-e-margins">
            <div class="ibox-title">
              <h3>
                <%= t('company.form.edit.title') %>
              </h3>
            </div>
            <div class="ibox-content">
              <% if @company.errors.any? %>
                <div id="error_explanation">
                  <h4><%= t('activerecord.errors.models.company.prohibited_save', count: @company.errors.count) %></h4>
                  <ul>
                    <% @company.errors.full_messages.each do |message| %>
                      <li><%= message %></li>
                    <% end %>
                  </ul>
                </div>
              <% end %>
              <% if @company.persisted? %>
                <%= hidden_field_tag :company_id, @company.id %>
              <% end %>
              <div class="form-group">
                <%= f.label :name, t('company.form.name') %>
                <%= f.text_field :name, required: true, class: 'form-control' %>
              </div>
              <div class="form-group">
                <%= f.label :company_type_id, t('company.form.company_type') %>
                <%= f.collection_select(:company_type_id,
                                        CompanyType.all,
                                        :id,
                                        :humanized_name,
                                        {prompt: t('common.select_placeholder')},
                                        {class: 'form-control', required: true}
                    ) %>
              </div>
              <h3><%= t('company.form.header.address') %></h3>
              <div class="form-group">
                <%= f.label :street_and_no, required: true %>
                <%= f.text_field :street_and_no, required: true, class: 'form-control' %>
              </div>
              <div class="form-group">
                <%= f.label :postal_code, required: true %>
                <%= f.text_field :postal_code, required: true, class: 'form-control' %>
              </div>
              <div class="form-group">
                <%= f.label :city, required: true %>
                <%= f.text_field :city, required: true, class: 'form-control' %>
              </div>
              <% if @company.is_eur? %>
                <hr>
                <h3><%= t('company.form.header.eur') %></h3>
                <div class="form-group">
                  <%= f.label :name_for_eur %>
                  <%= f.text_field :name_for_eur, class: 'form-control' %>
                  <%= f.label :first_name_for_eur %>
                  <%= f.text_field :first_name_for_eur, class: 'form-control' %>
                  <%= f.label :eur_income_type %>
                  <%= f.text_field :eur_income_type, class: 'form-control' %>
                  <%= f.label :eur_business_owner %>
                  <%= f.text_field :eur_business_owner, class: 'form-control' %>
                </div>
              <% end %>
              <hr>
              <h3><%= t('company.form.header.service_level.title') %></h3>
              <div class="form-group">
                <p>
                  <%= f.label :service_level_id, t('company.form.service_level'), required: true %>
                </p>
                <table class="table">
                  <tr>
                    <th><%= t('company.form.header.service_level.functionality') %></th>
                    <th class="centered"><%= t('company.form.header.service_level.free') %></th>
                    <th class="centered"><%= t('company.form.header.service_level.advanced') %></th>
                    <th class="centered"><%= t('company.form.header.service_level.expert') %></th>
                    <th class="centered"><%= t('company.form.header.service_level.pro') %></th>
                  </tr>
                  <tr>
                    <td colspan="5"></td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_1.1') %></b></td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_1.2') %></b></td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_2') %></b></td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_1.3') %></b></td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_3a') %></b></td>
                    <td></td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_3b') %></b></td>
                    <td></td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_16') %></b></td>
                    <td></td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_4') %></b></td>
                    <td class="centered"></td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_5') %></b></td>
                    <td></td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_6') %></b></td>
                    <td></td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_7') %></b></td>
                    <td></td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_8') %></b></td>
                    <td></td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_9') %></b></td>
                    <td colspan="2"></td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_10') %></b></td>
                    <td colspan="2"></td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_11') %></b></td>
                    <td colspan="2"></td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_12') %></b></td>
                    <td colspan="3"></td>
                    <td class="centered">x</td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_14') %></b></td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                    <td class="centered">x</td>
                  </tr>
                  <tr>
                    <td colspan="5"></td>
                  </tr>
                  <tr>
                    <td><b><%= t('company.form.header.service_level.functionality_15') %></b></td>
                    <td class="centered"><%= ServiceLevel::MAX_USER_COUNT[:free] %></td>
                    <td class="centered"><%= ServiceLevel::MAX_USER_COUNT[:advanced] %></td>
                    <td class="centered"><%= ServiceLevel::MAX_USER_COUNT[:expert] %></td>
                    <td class="centered"><%= t('company.form.header.service_level.functionality_random') %></td>
                  </tr>
                </table>
                <br>
                <%= f.select :service_level_id, options_for_service_levels(@company), prompt: t('common.select_placeholder'), class: 'form-control', required: true %>
                <br>
                <b><%= t('company.index.price.title') %></b>
                <ul>
                  <li><%= t('company.index.price.0.0') %>
                    <ul>
                      <li><%= t('company.index.price.0.1') %></li>
                      <li><%= t('company.index.price.0.2') %></li>
                    </ul>
                  </li>
                  <li><%= t('company.index.price.1') %></li>
                  <li><%= t('company.index.price.3') %></li>
                  <li><%= t('company.index.price.4') %></li>
                  <li><%= t('company.form.service_level_hint_2')%></li>
                </ul>
              </div>
              <hr>
              <h3><%= t('company.form.header.other_data') %></h3>
              <% if @company.is_eur? %>
                <div class="form-group">
                  <%= f.label :type_or_form %>
                  <%= f.text_field :type_or_form, class: 'form-control' %>
                </div>
                <div class="form-group">
                  <%= f.label :legal_form %>
                  <%= f.text_field :legal_form, class: 'form-control' %>
                </div>
              <% end %>
              <div class="form-group">
                <%= f.label :phone %>
                <%= f.text_field :phone, class: 'form-control' %>
              </div>
              <div class="form-group">
                <%= f.label :tax_number %>
                <%= t('company.form.tax_hint') %>
                <%= f.email_field :tax_number, required: true, class: 'form-control' %>
              </div>
              <div class="form-group">
                <%= f.label :amount_shareholders %>
                <%= f.text_field :amount_shareholders, class: 'form-control' %>
              </div>
              <div class="form-group">
                <%= f.label :creditor_id %>
                <%= f.text_field :creditor_id, class: 'form-control' %>
              </div>
              <div class="form-group">
                <%= f.label :vat_no %>
                <%= f.text_field :vat_no, required: true, class: 'form-control' %>
              </div>
              <div class="form-group">
                <%= f.label :vat_standard_rate, t('company.form.vat_standard_rate') %>
                <%= f.text_field :vat_standard_rate, class: 'form-control' %>
              </div>
              <div class="form-group">
                <%= f.label :registration_no %>
                <%= f.text_field :registration_no, class: 'form-control' %>
              </div>
              <div class="form-group">
                <%= f.label :daily_report_element_count %>
                (<%= t('activerecord.attributes.company.daily_report_element_count_hint')%>)
                <%= f.text_field :daily_report_element_count, class: 'form-control' %>
              </div>
              <div class="form-group">
                <%= f.label :eur_check %>
                <%= f.check_box :eur_check, class: 'form-control' %>
              </div>
              <div class="form-group">
                <%= f.label :fund_based %>
                <%= f.check_box :fund_based, class: 'form-control' %>
              </div>
              <hr>
              <h3><%= t('company.form.header.documents') %></h3>
              <div class="form-group">
                <%= f.label :notary_founding_document %>
                <% if @company.notary_founding_document.attached? %>
                  <%= link_to @company.notary_founding_document.filename, rails_blob_path(@company.notary_founding_document, disposition: 'attachment') %>
                <% end %>
                <%= f.file_field :notary_founding_document, class: 'form-control' %>
              </div>
              <div class="form-group">
                <%= f.label :registration_document %>
                <% if @company.registration_document.attached? %>
                  <%= link_to @company.registration_document.filename, rails_blob_path(@company.registration_document, disposition: 'attachment') %>
                <% end %>
                <%= f.file_field :registration_document, class: 'form-control' %>
              </div>
              <div class="form-group">
                <%= f.label :logo, t('company.form.logo') %>
                <% if @company.logo.attached? %>
                  <%= link_to @company.logo.filename, rails_blob_path(@company.logo, disposition: 'attachment') %>
                <% end %>
                <%= f.file_field :logo, class: 'form-control' %>
              </div>
              <div class="form-group">
                <%= f.label :invoice_logo, t('company.form.invoice_logo') %>
                <% if @company.invoice_logo.attached? %>
                  <%= link_to @company.invoice_logo.filename, rails_blob_path(@company.invoice_logo, disposition: 'attachment') %>
                <% end %>
                <%= f.file_field :invoice_logo, class: 'form-control' %>
              </div>
              <hr>
              <div class="form-group">
                <legend><%= t('company.form.tax_title') %></legend>
                <div class="form-group">
                  <%= f.label :chancellery_name, t('company.form.chancellery_name') %>
                  <%= f.text_field :chancellery_name, required: true, class: 'form-control' %>
                </div>
                <div class="form-group">
                  <%= f.label :chancellery_city, t('company.form.chancellery_city') %>
                  <%= f.text_field :chancellery_city, required: true, class: 'form-control' %>
                </div>
                <div class="form-group">
                  <%= f.label :chancellery_email, t('company.form.chancellery_email') %>
                  <%= f.text_field :chancellery_email, required: true, class: 'form-control' %>
                </div>
                <div class="form-group">
                  <%= f.label :chancellery_phone, t('company.form.chancellery_phone') %>
                  <%= f.text_field :chancellery_phone, required: true, class: 'form-control' %>
                </div>
              </div>
              <div class="form-group">
                <legend><%= t('company.form.show_in_overview_title') %></legend>
                <div class="form-group">
                  <%= f.label :show_in_overview, t('company.form.show_in_overview') %>
                  <%= f.check_box :show_in_overview, class: 'form-control' %>
                </div>
              </div>
              <% unless @company.agb_checked? && @company.system_invoice_checked? %>
                <div class="form-group">
                  <legend>AGB & Datenschutzerklärung</legend>
                  <%= link_to t('company.form.agb_title'), '/agb.pdf', target: '_blank' %>
                  <%= f.label :agb_checked, t('company.form.agb_checked') %>
                  <%= f.check_box :agb_checked, required: true, class: 'form-control' %>
                </div>
                <div class="form-group">
                  <%= f.label :system_invoice_checked, t('company.form.system_invoice_checked') %>
                  <%= f.check_box :system_invoice_checked, class: 'form-control' %>
                </div>
              <% end %>
              <div class="form-group">
                <%= f.submit t("common.same_pattern_buttons.#{@company.new_record? ? :create : :update}", model_name: model_name_translation(:company)), class: 'btn btn-primary' %>
              </div>
            </div>
          </div>
        </div>
      </div>
    <% end %>

  </div>
</div>

更新

根据 @engineersmnky 的评论中的要求,我正在添加

home/companies_controller.rb
的控制器代码,但我认为它相当简单:

class Home::CompaniesController < HomeController
  before_action :service_level_is_free?, except: [:index, :new, :create, :do_accounting, :request_authority]
  skip_before_action :verify_user_has_accounting_authority?, except: [:show, :edit, :update, :destroy]

  def appointments
...
  end

  def index
...
  end

  def show
...
  end

  def new
...
  end

  def create

.... 结束

  def edit
    load_company
    @title = "#{t('titles.companies.edit')} #{@company.name}"
  end
...
end
html ruby-on-rails view erb
1个回答
0
投票

如果您使用 rails_ujs(适用于 jQuery 的不显眼脚本适配器),请确保在

jquery
中的
app/assets/javascripts/application.js
之后需要它:

//= require jquery3
//= require jquery_ujs
© www.soinside.com 2019 - 2024. All rights reserved.