Displaying simple_form error messages in top

前端 未结 1 2028
悲哀的现实
悲哀的现实 2021-02-07 06:55

I have the following two _forms:

user form

<%= simple_form_for(@user, :url => @target) do |f| %>
  <% if @user.errors.any? %         


        
相关标签:
1条回答
  • Just add error: false to your inputs this will not clear the css but will clear the inline errors

    f.input error: false or :error => false
    

    Edit:

    From http://ruby.railstutorial.org/book/ruby-on-rails-tutorial

    /app/views/shared/_error_messages.html.erb

     <% if object.errors.any? %>
      <div id="error_explanation">
       <div class="alert alert-error">
         The form contains <%= pluralize(object.errors.count, "error") %>.
       </div>
      <ul>
       <% object.errors.full_messages.each do |msg| %>
        <li>* <%= msg %></li>
       <% end %>
     </ul>
    </div>
    <% end %>
    

    in VIEW

    <%= form_for(@user) do |f| %>
      <%= render 'shared/error_messages', object: f.object %>
    
      <%= f.label :name %>
      <%= f.text_field :name %>
    
      <%= f.label :email %>
      <%= f.text_field :email %>
    
      <%= f.label :password %>
      <%= f.password_field :password %>
    
      <%= f.label :password_confirmation, "Confirmation" %>
      <%= f.password_field :password_confirmation %>
    
      <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
    <% end %>
    
    0 讨论(0)
提交回复
热议问题