(EC site) Validation when entering order information

Prerequisites

Enter order information on the EC site on the EC site ==> I want to validate by entering the order information between the order confirmation screens.

1. Frequently used validation

app/models/book.rb


validates :title, presence: true

Validation that is often used such as There is a column called title in the books table If the title column is blank when it is saved in the database, that is, when @ book.save (book_params), validation will be applied. However, order information input on the EC site ==> Order confirmation screen ==> When implementing with the flow of order confirmation (.save is performed here), order information input ==> Since .save does not occur between the order confirmation screens, the validation applied to the model is rendered. Since it cannot be called with, it is necessary to write a conditional branch in the if statement and use flash [: notice].

2. Implementation of if statement

app/controllers/order_controllers.rb


when 3
        if    params[:order][:new_add][:postal_code] == "" && params[:order][:new_add][:address] == "" && params[:order][:new_add][:name] == ""
              flash[:notice] = "All new destinations have not been entered"
              redirect_to new_order_path
        elsif params[:order][:new_add][:postal_code] == ""
              flash[:notice] = "No zip code entered"
              redirect_to new_order_path

        elsif params[:order][:new_add][:address] == ""
              flash[:notice] = "Address not entered"
              redirect_to new_order_path
        elsif params[:order][:new_add][:name] == ""
              flash[:notice] = "No address has been entered"
              redirect_to new_order_path
        else
            @order.postal_code = params[:order][:new_add][:postal_code]
            @order.address = params[:order][:new_add][:address]
            @order.name = params[:order][:new_add][:name]
        end
 end

3. Code description

First, using the case statement, three radio buttons are written in order / new.html.erb and three patterns are branched at when. And if you select the third radio button, you can enter a new delivery address when entering order information. [: new_add] is a parameter for entering a new shipping address when entering order information.

app/controllers/order_controllers.rb


params[:Value sent in the form] == ""
flash[:notice] ="Honya Honya"

You can send a flash message when it is blank.

4. After this implementation

Initially, it was set to == nil and the value was not returned, but when I looked at the terminal, it was "", so I changed the conditional statement and it worked. When connecting conditional statements with &&, it is necessary to equal each time instead of putting only one equal at the end.

5. Reference site

https://qiita.com/GreenFingers_tk/items/ed5219e1e0cdd5e5d1b1#new

Recommended Posts

(EC site) Validation when entering order information
Create an EC site with Rails 5 ⑩ ~ Create an order function ~
Error when deploying EC2