0

我有一个包含一堆字段的相当基本的表单。这是一段摘录:

<%= form_for(@subject) do |f| %>

  <% if @subject.errors.any? %>
    <div id="error_explanation">
    <h2><%= t('activerecord.errors.subject.header', :model => Subject.model_name.human, :count => @subject.errors.count) %></h2>

      <ul>
      <% @subject.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="group">
    <fieldset class="left">
      <div class="field">
        <%= f.label "Anrede" %>

        <%= f.radio_button :title, "Herr" %>
        <%= f.label :title_herr, "Herr", class: "radio" %>

        <%= f.radio_button :title, "Frau" %>
        <%= f.label :title_frau, "Frau", class: "radio" %>
      </div>
      <div class="field">
        <%= f.label :name %>
        <%= f.text_field :firstname, class: "m", placeholder: "Vorname" %>

        <%= f.text_field :lastname, class: "m", placeholder: "Nachname" %>
      </div>
      <div class="field">
        <%= f.label "Stadt" %>
        <%= f.select :city, Subject::CITIES, prompt: "- Bitte wählen -" %>
      </div>

控制器主要还是来自脚手架:

  # POST /subjects
  # POST /subjects.json
  def create
    @subject = Subject.new(subject_params)

    respond_to do |format|
      if @subject.save
        format.html { }
        format.json { render action: 'create', status: :created, location: @subject }
      else
        format.html { render action: 'new' }
        format.json { render json: @subject.errors, status: :unprocessable_entity }
      end
    end
  end

但是,当提交无效的表单时,它会显示#error_explanation-div,但不会div.field_with_error像以前那样包装错误的字段。真的不知道是什么改变让它崩溃了。

4

1 回答 1

1

我不确定 Rails 是否提供您期望的行为,但您肯定可以查看simple_formformtastic gem(我推荐前者)

于 2013-08-08T09:54:58.030 回答