How to Highlight Fields on Rails Validation Errors

后端 未结 3 1579
梦毁少年i
梦毁少年i 2020-12-31 15:11

How do you display form field highlighting for fields that fail validation errors in Rails 3.1? I know that scaffolding automatically generates the css and controller code t

3条回答
  •  庸人自扰
    2020-12-31 15:29

    Rails now have a nice trick up its sleeve..When an error occurs rails put a div with a class .field_with_errors around the error fields. So now you can just target that class and add styling.

    To focus on the input you can do

    .field_with_errors input{
      border: 1px solid red !important;
    }
    

    this css will put a nice red line around the input element while important! will overwrite any existing styles.

提交回复
热议问题