我有以下简单形式:
<% 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 %>
显示只有一个字段的形式:weight。
通常它呈现如下:
<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:weight正确显示错误消息,但标签和文本字段没有包含在类fieldWithError的div中,正如我在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>
为什么我没有获得这些div的任何想法?我已经安装了formtastic并且它正在以其他形式使用,但据我所知,不应该干扰这种形式。
更新:为了确定,我打印出debug(@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
更新:模型
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
端
答案 0 :(得分:1)
这是Formtastic中的一个错误。它已经修复,但似乎目前没有发布的Formtastic版本有修复。
我自己的错误报告位于http://github.com/justinfrench/formtastic/issues/closed/#issue/132
可以在http://github.com/grimen/formtastic/commit/2b81d9af385dadf8b37dc14f387afe3d43e4958a
上看到该修复程序最终问题是使用了来自github的justinfrench-formtastic,它已经过时并被放弃而不是来自gemrofter的formtastic。
答案 1 :(得分:0)
您可能需要附上您的标签&amp;块标记中的字段,如p或div。
<p>
<%= f.label :weight %>:
<%= f.text_field :weight, :size => 5 %> kg.
</p>
这样rails可以将错误放回原处,否则它会像现在一样回到表单中。