У меня есть следующий простой вид:Где находился div class = fieldWithError?
<% form_for(@weight) do |f| %>
<%= f.error_messages %>
<%= f.label :weight %>:
<%= f.text_field :weight, :size => 5 %> kg.
<%= f.submit "Add weight" %>
<%= f.error_message_on :weight %>
<% end %>
, который отображает форму только одного поля: вес.
Обычно это делает так:
<form action="/weights" class="new_weight" id="new_weight" method="post">
<div style="margin:0;padding:0;display:inline"><input name="authenticity_token" type="hidden" value="jYoVJkDnv4a1DMGnelJpGPElbH0XWKPNlESTt9GvzdA=" /></div>
<label for="weight_weight">Weight</label>:
<input id="weight_weight" name="weight[weight]" size="5" type="text" /> kg.
<input id="weight_submit" name="commit" type="submit" value="Add weight" />
</form>
, который прекрасно. Когда я отправляю эту форму без установки веса, я получаю ошибку проверки. f.error_messages и f.error_messages_on: вес правильно отображает сообщения об ошибках, но метка и текстовое поле не окружены в div с классом fieldWithError, как я обычно ожидаю в формах в Rails. Вместо этого я получаю это:
<form action="/weights" class="new_weight" id="new_weight" method="post">
<div style="margin:0;padding:0;display:inline"><input name="authenticity_token" type="hidden" value="jYoVJkDnv4a1DMGnelJpGPElbH0XWKPNlESTt9GvzdA=" /></div>
<div class="errorExplanation" id="errorExplanation">
<h2>1 error prohibited this weight from being saved</h2>
<p>There were problems with the following fields:</p>
<ul><li>Weight can't be blank</li></ul>
</div>
<label for="weight_weight">Weight</label>:
<input id="weight_weight" name="weight[weight]" size="5" type="text" /> kg.
<input id="weight_submit" name="commit" type="submit" value="Add weight" />
<div class="formError">can't be blank</div>
</form>
для справки, что я должен был получить это:
<form action="/weights" class="new_weight" id="new_weight" method="post">
<div style="margin:0;padding:0;display:inline"><input name="authenticity_token" type="hidden" value="jYoVJkDnv4a1DMGnelJpGPElbH0XWKPNlESTt9GvzdA=" /></div>
<div class="errorExplanation" id="errorExplanation">
<h2>1 error prohibited this weight from being saved</h2>
<p>There were problems with the following fields:</p>
<ul><li>Weight can't be blank</li></ul>
</div>
<div class="fieldWithErrors"><label for="weight_weight">Weight</label></div>:
<div class="fieldWithErrors"><input id="weight_weight" name="weight[weight]" size="5" type="text" /></div> kg.
<input id="weight_submit" name="commit" type="submit" value="Add weight" />
<div class="formError">can't be blank</div>
</form>
Любые идеи, почему я не получаю эти дивы? У меня установлен formtastic, и он используется в других формах, но насколько я знаю, это не должно мешать этой форме.
Update: просто чтобы быть уверенным, я распечатал отладки (@weight), он имеет ошибки:
--- &id002 !ruby/object:Weight attributes: created_at: updated_at: weight: measured_on: &id001 !timestamp at: "2009-11-22 01:30:13.522589 +01:00" "@marshal_with_utc_coercion": false user_id: 1 attributes_cache: measured_on: *id001 changed_attributes: measured_on: user_id: errors: !ruby/object:ActiveRecord::Errors base: *id002 errors: weight: - !ruby/object:ActiveRecord::Error attribute: :weight base: *id002 message: :blank options: {} type: :blank new_record: true
Update: модель
class Weight < ActiveRecord::Base
belongs_to :user
validates_presence_of :weight, :measured_on
attr_accessible :weight, :measured_on
def after_initialize
self.measured_on ||= Time.now
end
конец
Примечание: я удалил formtastic и начал работать правильно. – Pablo