Objective

Over the weekend I spent some time improving the visuals of the site. I came across a template for men's fashion and decided to create a men's fashion site as I had been shopping for a tuxedo prior to the program.

Today we began Phase 2 of the e-commerce project. This phase involves creating a shopping cart and building all of the necessary functionalities around it. We were given hints on how to add a shopping cart upfront, but had to figure out on our own how to increase the shopping cart's functionalities.

I managed to complete Step 1: adding in the shopping cart, creating an index page of cart items, and has a total sum of all the cart items' price. For the remaining 6 tasks, I only managed to complete 2 of them today: clearing shopping cart, and removing an individual item from shopping cart.

Reflective

During today's assignment I had to do a lot more thinking rather than simply copy/pasting codes I had written for previous projects. I was stuck trying to create a remove_item_from_cart action by reverse-engineering the commands used to add_item_to_cart. After getting stuck on this for an hour or so, I decided to go ahead and create a controller for cart_items and used the destroy method to remove the cart item instead.

In addition, when I added the shopping cart function, I encountered the database issue once again, with reported error:

ActionView::Template::Error (SQLite3::SQLException: no such column: cart_items.cart_id: SELECT COUNT(*) FROM "products" INNER JOIN "cart_items" ON "products"."id" = "cart_items"."product_id" WHERE "cart_items"."cart_id" = ?):

After trying the rake db:migrate command multiple times to no avail, I followed the database migration fix that I had written about earlier and resolved the issue.

Interpretive

Adding the shopping cart was the firt time that we are departing from the plain CRUD methods when creating a model. In building the clear_cart method, we did not use the destroy method, but rather a post action created in the model, by doing the following:

  1. Config route:

    config/routes.rb
    resources :carts do
    collection do
        post :clear_cart
    end
    end
    
  2. Define in controller

    app/controllers/application_controller.rb
        def clear_cart
        current_cart.cart_items.each do |item|
            item.destroy
        end
        redirect_to :back
    end
    

    (Careful not to place this under the "private" section otherwise it won't be callable globally)

  3. Adding button in view

    app/views/carts/index.html.erb
    <%= link_to("Clear Cart", clear_cart_carts_path, method: :post, class: "btn btn-lg btn-primary pull-right") %>
    

The specific route for the already defined clear_cart action can be found using rake routes command:

I realize now that there are actions beyond CRUD methods that we can define on our own as long as we get the logic right.

Decisional

In conversing with others in the class, I realize that though I can build certain functions by Googling and referencing prior projects, there were a lot of basic Ruby terminologies and logic that I am still unfamiliar with, which hinders my ability to discuss with classmates on coming up with solutions. I will need to spend more time on Ruby basics.