显示simple_form消息

时间:2013-06-23 12:59:19

标签: ruby-on-rails simple-form

我在Rails项目中使用了Simple_form gem,在我进入现场之前有人正在处理它,所以我对它不太熟悉。

在哪里可以更改显示错误消息的位置?目前它们出现在文本框的下方,如下所示:

enter image description here

基本上,我的Rails项目中的位置是被告知,'在文本框下显示错误消息?'

我可以看到:

<%= f.input :name, :required => true, :label_html => { :class => 'edit_form_titles' }, :error_html => { :class => 'cant_be_blank'} %>

但无论我对该代码做了什么,它仍然在文本框下留下一个空格。我希望消息显示在顶部,文本框下方没有空格。我的初始化文件夹文件夹中有一个文件simple_form.rb,它可能处理它,但不知道在哪里查看或要更改什么。这是simple_form 2.0.2。

1 个答案:

答案 0 :(得分:1)

我设法将错误显示在输入框上方的标签中。

使用下面的代码,我给了我的错误一个类,可以格式化为定位等,但在输入框下面总是有一个空白的div或者某些东西,它将其下面的其他输入框放在关节之外

<%= f.input :name, :required => true, :label_html => { :class => 'edit_form_titles' }, :error_html => { :class => 'cant_be_blank'} %>

在我的initializers / simple_form.rb中有:

  config.wrappers :bootstrap, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|
    b.use :html5
    b.use :placeholder
    b.use :label
    b.wrapper :tag => 'div', :class => 'controls' do |input|
      input.use :input
      input.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
      input.use :hint,  :wrap_with => { :tag => 'p', :class => 'help-block' }
    end
  end

我将其更改为:

  config.wrappers :bootstrap, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|
    b.use :html5
    b.use :placeholder
    b.wrapper :tag => 'div', :class => 'label-error' do |input|
      b.use :label
      b.use :error, :wrap_with => { :tag => 'span', :class => 'help-block' }
    end
    b.wrapper :tag => 'div', :class => 'controls' do |ba|
      ba.use :input
      ba.use :hint,  :wrap_with => { :tag => 'p', :class => 'help-block' }
    end
  end

摆脱了输入框下方的空白空格,我可以格式化我的cant_be_blank类,使文本显示在我标签中的文本旁边。