[RUBY] Description to avoid skipping to the URL of the product purchased by the index action even if you hit the URL directly

How to redirect to the index page even if you hit the URL directly on the page of the purchased product

Why

How do you do it with a certain flea market app? I wrote an article hoping that it would be useful for school students because it was solved if I touched various things myself!

Describe where to move the URL in the index action of the controller.

orders_controller.rb


def index
    @item = Item.find(params[:item_id])
    @address = OrderAddress.new
    if @address.save
        redirect_to root_path
    end
end

Pay attention to the if statement on the 4th line and redirect_to on the 5th line!

Store the @ address </ font> (two models grouped by the form object) defined in the 3rd line as an instance, and in the 4th line When the record is saved, it is defined as </ font>. And if it is saved (if there is already information in the record) redirect_to </ font>, "root_path", that is, transition to the top page ~ is the description on the 5th line.

This is!

fe573d5f79ce1ddfcf7d42156ec0f7d1.png

If this happens, it's OK !!

12ba866f3a3964bd80891b8fc643779e.png

bonus

By the way

  • items = Product information
  • addresses = Buyer Information
  • orders = product ID and userID of who bought it
  • users = all users

Delivered with the name sense of!

That's all from the scene!