Styling form error message - bootstrap/rails

前端 未结 8 618
陌清茗
陌清茗 2021-02-01 18:38

The error messages for my rails form look terrible with bootstrap. Does anyone know a solution for better (nice looking) error messages? I use Rails and Bootstrap.

My fo

8条回答
  •  温柔的废话
    2021-02-01 19:13

    A little late I realize, but I just ran into this today with Rails 4 and Bootstrap 3, I ended up making a view helper to display errors using a panel:

    Rails 4 / Bootstrap 3

    def errors_for(object)
        if object.errors.any?
            content_tag(:div, class: "panel panel-danger") do
                concat(content_tag(:div, class: "panel-heading") do
                    concat(content_tag(:h4, class: "panel-title") do
                        concat "#{pluralize(object.errors.count, "error")} prohibited this #{object.class.name.downcase} from being saved:"
                    end)
                end)
                concat(content_tag(:div, class: "panel-body") do
                    concat(content_tag(:ul) do
                        object.errors.full_messages.each do |msg|
                            concat content_tag(:li, msg)
                        end
                    end)
                end)
            end
        end
    end
    

    Rails 4 / Bootstrap 4 Beta

    def errors_for(object)
        if object.errors.any?
            content_tag(:div, class: "card border-danger") do
                concat(content_tag(:div, class: "card-header bg-danger text-white") do
                    concat "#{pluralize(object.errors.count, "error")} prohibited this #{object.class.name.downcase} from being saved:"
                end)
                concat(content_tag(:div, class: "card-body") do
                    concat(content_tag(:ul, class: 'mb-0') do
                        object.errors.full_messages.each do |msg|
                            concat content_tag(:li, msg)
                        end
                    end)
                end)
            end
        end
    end
    

    Rails 4 / Bootstrap 4 Beta List Group Variation

    def errors_for(object)
        if object.errors.any?
            content_tag(:div, class: "card border-danger") do
                concat(content_tag(:div, class: "card-header bg-danger text-white") do
                    concat "#{pluralize(object.errors.count, "error")} prohibited this #{object.class.name.downcase} from being saved:"
                end)
                concat(content_tag(:ul, class: 'mb-0 list-group list-group-flush') do
                    object.errors.full_messages.each do |msg|
                        concat content_tag(:li, msg, class: 'list-group-item')
                    end
                end)
            end
        end
    end
    

    I dropped it in application_helper and call it in my form views

    <%= errors_for(@user) %>
    

    Maybe someone will stumble upon this and find it useful.

提交回复
热议问题