加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 表单元素的自定义HTML错误包装

发布时间:2020-12-17 03:30:07 所属栏目:百科 来源:网络整理
导读:我想找到一种方法来自定义默认错误html div class="field_with_errors"/div 参加我自己的课程: div class="clearfix error" label for="errorInput"Input with error/label div class="input" input class="xlarge error" id="errorInput" name="errorInput
我想找到一种方法来自定义默认错误html

<div class="field_with_errors"></div>

参加我自己的课程:

<div class="clearfix error">
        <label for="errorInput">Input with error</label>
        <div class="input">
          <input class="xlarge error" id="errorInput" name="errorInput" size="30" type="text">
          <span class="help-inline">Small snippet of help text</span>
        </div>
      </div>

我相信,我从2007年发现了这个使用Rails 2的railscast. http://railscasts.com/episodes/39-customize-field-error.似乎Rails 3可能有更友好的方式来自定义这个HTML?

此外,它没有显示只是像我想要的那样直接向输入添加错误类的方法.

解决方法

您发布的链接中说明的方法今天仍在使用 vanilla form builders in Rails.

因此,如果您想像提到的那样包装输入,请创建一个覆盖environment.rb文件中的ActionView :: Base.field_error_proc的方法,例如:

ActionView::Base.field_error_proc = Proc.new do |html_tag,instance|
  if instance.error_message.kind_of?(Array)
    %(<div class="form-field error">#{html_tag}<small class="error">&nbsp;
      #{instance.error_message.join(',')}</small></div).html_safe
  else
    %(<div class="form-field error">#{html_tag}<small class="error">&nbsp;
      #{instance.error_message}</small></div).html_safe
  end
end

在上面的代码中,我将输入(#{html_tag})包装在< div class =“form-field error> ..< / div>中,这是ZURB Foundation使用的默认值.我也在使用a< small class =“error”> …< / small tag(也是默认的基础),用于显示输入下方的消息. 但是,我建议使用像simple_form这样的表单构建器gem.它会清除您的视图代码,并允许您需要的自定义级别.

查看here上的railscast.

祝好运!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读