Rails validation error messages: Displaying only one error message per field

后端 未结 13 882
情话喂你
情话喂你 2020-12-07 17:08

Rails displays all validation error messages associated with a given field. If I have three validates_XXXXX_of :email, and I leave the field blank, I get three

13条回答
  •  抹茶落季
    2020-12-07 17:49

    I wrote a custom helper

    def display_error(field)
        if @user.errors[field].any?
            raw @user.errors[field].first+"
    " end end

    and then I use it in view under the text field like so

    <%= display_error(:password) %>
    

提交回复
热议问题