6

<div class="fieldWithErrors">検証エラーのあるラップされた選択タグを取得しないのは正常な動作ですか? 個人的には、選択タグを他のフォーム タグ (input、textarea) とは異なる方法で扱う必要がある理由はわかりません。

そのフィールドのエラーとメソッドが表示されます。error_messages_forerror_message_on

PS。ActionView::Base.field_error_procdiv の代わりにスパン タグを取得するために少し変更しましたが、それは問題ではありません。

ActionView::Base.field_error_proc = Proc.new { |html_tag, instance|
   #if I puts html_tag here I only get the <input> tags
   "<span class=\"fieldWithErrors\">#{html_tag}</span>"
}
4

5 に答える 5

4

(少なくとも私にとって) 問題は、検証が実際にオンになったときのキーをオブジェクトでf.select :whatever_id探していたことでした。object.errors:whatever_id:whatever:whatever_id

変更することで、この厄介な問題を回避しました

object.errors.on(@method_name)

object.errors.on(@method_name) || object.errors.on(@method_name.gsub(/_id$/, ''))

これが差分です(Rails 2.3.4に対する):

diff --git a/vendor/rails/actionpack/lib/action_view/helpers/active_record_helper.rb b/vendor/rails/actionpack/lib/action_view/helpers/active_record_helper.rb
index 541899e..5d5b27e 100644
--- a/vendor/rails/actionpack/lib/action_view/helpers/active_record_helper.rb
+++ b/vendor/rails/actionpack/lib/action_view/helpers/active_record_helper.rb
@@ -247,7 +247,7 @@ module ActionView
       alias_method :tag_without_error_wrapping, :tag
       def tag(name, options)
         if object.respond_to?(:errors) && object.errors.respond_to?(:on)
-          error_wrapping(tag_without_error_wrapping(name, options), object.errors.on(@method_name))
+          error_wrapping(tag_without_error_wrapping(name, options), object.errors.on(@method_name) || object.errors.on(@method_name.gsub(/_id$/, '')))
         else
           tag_without_error_wrapping(name, options)
         end
@@ -256,7 +256,7 @@ module ActionView
       alias_method :content_tag_without_error_wrapping, :content_tag
       def content_tag(name, value, options)
         if object.respond_to?(:errors) && object.errors.respond_to?(:on)
-          error_wrapping(content_tag_without_error_wrapping(name, value, options), object.errors.on(@method_name))
+          error_wrapping(content_tag_without_error_wrapping(name, value, options), object.errors.on(@method_name) || object.errors.on(@method_name.gsub(/_id$/, '')))
         else
           content_tag_without_error_wrapping(name, value, options)
         end
于 2009-12-17T07:18:59.923 に答える
3

select タグがその Proc に含まれていない理由がわからなかったので、ほぼ同じことを行うヘルパー メソッドを作成しました。

def field_with_error(object, method, &block)
  if block_given?
    if error_message_on(object, method).empty?
      concat capture(&block)
    else
      concat '<span class="fieldWithErrors">' + capture(&block) + '</span>'
    end
  end
end

私は自分の見解でそれを次のように使用します:

<% field_with_error @some_object, :assoc do %>
  <%= f.select(:assoc_id, @associations.collect {|assoc| [ asoc.name, assoc.id ] }) %>
<% end %>

誰かがそれを行うためのより良い方法またはよりクリーンな方法を知っている場合は、提案を受け付けています。

于 2009-04-25T11:34:11.307 に答える
2

これに対処しているように見えるこのブログ投稿を見つけました。

http://blog.invalidobject.com/2007/09/16/rails-error-wrapping-for-select-input-fields-of-referenced-models

お役に立てば幸いです。

于 2009-04-14T14:13:51.903 に答える
1

これが私がその問題を解決する方法です。

特定の「field_with_errors」選択ラッパーを作成します。

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
   if html_tag =~ /^<input/
     %{<div class="field_with_errors">#{html_tag}<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe
   elsif html_tag =~ /^<select/
 %{<div class="field_with_errors" id="select-error">#{html_tag}</div>}.html_safe
   else
     %{<div class="field_with_errors">#{html_tag}</div>}.html_safe
   end
end

CSS:

#select-error {
    border: 1px solid red;
    overflow-y: auto;
    overflow-x: hidden;
}

そして、検証を :whatever ではなく :whatever_id に変更します

validates :whatever_id, :presence => true

私は忘れていました、選択:

f.collection_select(:whatever_id, Whatever.all, :id, :name, prompt: t(:please_choose))
于 2012-02-14T14:00:30.923 に答える
0

別の方法として、メソッドまたはコントローラー レベル、または environment.rb に挿入できます。

ActionView::Base.field_error_proc = proc { |入力、インスタンス| 入力}

于 2009-08-11T20:56:39.100 に答える