2009-11-22 25 views
0

我有以下簡單形式: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:權重正確顯示錯誤消息,但標籤和文本字段未包含在類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,並以其他形式使用,但據我所知,不應該干擾這種形式。

更新:只是爲了保險起見,我打印出來的調試(@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

注:我刪除了formtastic,它開始正常工作。 – Pablo 2009-11-22 00:45:40

回答

0

你可能需要將標籤&字段放在塊標籤(如p或div)中。

<p> 
    <%= f.label :weight %>: 
    <%= f.text_field :weight, :size => 5 %> kg. 
</p> 

這樣的軌道有一個地方滑錯誤放回,否則回落到形式,因爲它現在正在做。

+0

可能不必是一個「塊」標籤,任何機箱都應該這樣做。 – nowk 2009-11-22 00:39:44

+0

這並沒有幫助。事實上,rails會將標籤放在一個div內,並將文本字段放在另一個div內。我不認爲rails可以修改HTML代碼,比如p,它已經寫在視圖中;它只能生成更多的代碼。 – Pablo 2009-11-22 00:40:53

+0

我的歉意,我應該仔細閱讀你的文章。我的腦海裏想着別的東西。但你似乎已經明白了。我不熟悉那個插件或者你可以配置什麼讓它們回來,但是如果你想自己做一些自定義的工作,你可以看看ActionView :: Base.field_error_proc。 – nowk 2009-11-22 01:24:12