Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails验证|向周围添加类<;部门>;_Ruby On Rails_Validation - Fatal编程技术网

Ruby on rails Rails验证|向周围添加类<;部门>;

Ruby on rails Rails验证|向周围添加类<;部门>;,ruby-on-rails,validation,Ruby On Rails,Validation,问题。错误验证的默认行为是在输入字段周围使用FIELDWERROR样式的div,如下所示 <div class="type-text" id="pre_negotiation_value_div"> <label for="contract_budget_holder">Budget Holder</label> <div class="fieldWithErrors"> <input id="contract_budget_holder"n

问题。错误验证的默认行为是在输入字段周围使用FIELDWERROR样式的div,如下所示

<div class="type-text" id="pre_negotiation_value_div">
<label for="contract_budget_holder">Budget Holder</label>
<div class="fieldWithErrors">
<input id="contract_budget_holder"name="contract[budget_holder]" size="30" type="text" value="" />
</div>
</div>

预算负责人
我试图实现的是,用一个已分类的div标记包围标签和输入字段,如下所示:

<div class="type-text fieldWithErrors" id="pre_negotiation_value_div">
<label for="contract_budget_holder">Budget Holder</label>      
<input id="contract_budget_holder"name="contract[budget_holder]" size="30" type="text" value="" />      
</div>

预算负责人
有没有办法做到这一点?能否将javascript连接到ActionView::Base.field\u error\u proc中

我是Rails新手,如果这非常简单,我向您道歉

谢谢你的帮助


Jonathan

想做你想做的事并不容易,但如果你有这样的ERB:

<div id="pre_negotiation_value_div" class="type-text">
  <%= f.label :name %>
  <%= f.text_field :name %>
</div>          
<div id="pre_negotiation_value_div" class="type-text">
  <div class="fieldWithErrors"><label for="foo_name">Name</label></div>
  <div class="fieldWithErrors"><input id="foo_name" name="foo[name]" size="30" type="text" value="" />
</div>
class ActionView::Helpers::FormBuilder
  def labeled_input(method, options={}, &block)
    (options[:class] ||= "") << " fieldWithErrors" if @object.errors.on(method)
    ActionView::Helpers::InstanceTag.send(:alias_method, :original_error_wrapping, :error_wrapping)
    ActionView::Helpers::InstanceTag.send(:define_method, :error_wrapping,
      Proc.new {|html_tag, has_error| html_tag})
    @template.concat(@template.content_tag(:div, @template.capture(&block), options))
  ensure
    ActionView::Helpers::InstanceTag.send(:alias_method, :error_wrapping, :original_error_wrapping)
    ActionView::Helpers::InstanceTag.send(:remove_method, :original_error_wrapping)
  end
end
<% f.labeled_input :name, :id => "pre_negotiation_value_div", :class => "type-text" do %>
  <%= f.label :name %>
  <%= f.text_field :name %>
<% end %>

RAILS 3更新

我正在将我的应用程序更新到rails 3。如果您使用过此方法并且正在升级,您将收到一些弃用警告,并且表单字段会出现两次或多次

要解决这些问题,请执行以下操作:

1) 更改标记为_input.rb的检查错误的行。这将停止不推荐警告访问错误:

(options[:class] ||= "") << " fieldWithErrors" unless @object.errors[method].empty?

3) 在您的视图文件中,在您调用标记的输入的地方,使用那个工作人员。谢谢我从来没有想到过。
@template.content_tag(:div, @template.capture(&block), options)