Home > Software design >  Using validations and pluralize in views to show errors count for forms rails 7
Using validations and pluralize in views to show errors count for forms rails 7

Time:12-01

i am trying to display a message for whenever someone does not fill the form completely and press the submit button!

Example: when someone clicks the submit button without filling the Name and Email field in the form, it should display

2 Errors Prohibited from submitting the form!
. Name can't be blank
. Email can't be blank

but it is not being displayed whenever we submit the form with empty fields

can someone explain me the reason why it is not working?

Orders_controller.rb

class OrdersController < ApplicationController

    # GET to /orders/new
    def new
        @order = Order.new
    end

    # POST to /orders
    def create
        @order = Order.new(order_params)

        respond_to do |format|
            if @order.save!
                format.html{ redirect_to root_url, notice: "Order Succesfully Submitted!" }

            else
                format.html{ render :new, status: :unprocessable_entity }
            end
        end
    end

    private
        def order_params
            params.require(:order).permit(:first_name, :last_name, :phone_number, :email, :paper_size, :color, :paper_style, :quantity, :description, files: [] )
        end
end

_form.html.erb (i already rendered the partial in new.html.erb by <%= render 'form', order: @order%>

<div >
    <h1 >Order From Home!</h1>
    
    <div >
        <div >

            <%= form_with(model: order) do |f| %> 

                <% if order.errors.any? %>

                    <div style="color: red">
                        <h3><%= pluralize(order.errors.count,"errors")%> Prohibited from submitting the form! <br/> 
                        All Fields Are Required!</h3>

                        <ul>
                            <% order.errors.each do |error| %>
                                <li><%= error.full_message %></li>
                            <% end %>
                        </ul>
                    </div>

                <% end %> 

                <%= f.label :first_name%>
                <%= f.text_field :first_name, class:"form-control" %><br/>

                <%= f.label :last_name %>
                <%= f.text_field :last_name, class:"form-control" %><br/>
            

                <%= f.label :phone_number %>
                <%= f.text_field :phone_number, class:"form-control" %><br/>

            
                <%= f.label :email %>
                <%= f.text_field :email, class:"form-control" %><br/>

                <%= f.label :files %>
                <%= f.file_field :files, multiple: true %><br/>
            

                <%= f.label :paper_size %>
                <%= f.select :paper_size, ['A4', 'B4'], { prompt: 'Select' }, class:'form-select' %><br/>

                <%= f.label :color %>
                <%= f.select :color, ['Black & White', 'Color'], { prompt: 'Select' }, class:'form-select' %><br/>

                <%= f.label :paper_style %>
                <%= f.select :paper_style, ['Black to Back', 'Side to Side'], { prompt: 'Select' }, class:'form-select' %><br/>

                <%= f.label :quantity %>
                <%= f.number_field :quantity, class:'form-control' %><br/>

                <%= f.label :description %>
                <%= f.text_area :description, class:"form-control" %><br/>

                <div >
                    <%= f.submit %>
                </div>
            <% end %>
            
        </div>     
    </div>
</div>

CodePudding user response:

@order.save! raises validation errors. You don't need bang method if you want to render such errors, just use @order.save instead

form_with sends XHR request. If you need to render something, you can disable Turbo for this form

<%= form_with(model: order, data: { turbo: false }) do |f| %>
  • Related