| @@ -37,6 +37,10 @@ | |||||
| margin-left: 100px; | margin-left: 100px; | ||||
| margin-top: 0.5em; | margin-top: 0.5em; | ||||
| margin-bottom: 0.8em; | margin-bottom: 0.8em; | ||||
| form, div { | |||||
| display: inline; | |||||
| } | |||||
| } | } | ||||
| .price { | .price { | ||||
| @@ -1,6 +1,10 @@ | |||||
| class LineItemsController < ApplicationController | class LineItemsController < ApplicationController | ||||
| include CurrentCart | |||||
| before_action :set_cart, only: [:create] | |||||
| before_action :set_line_item, only: [:show, :edit, :update, :destroy] | before_action :set_line_item, only: [:show, :edit, :update, :destroy] | ||||
| # TODO: Create counter from play time. | |||||
| # GET /line_items | # GET /line_items | ||||
| # GET /line_items.json | # GET /line_items.json | ||||
| def index | def index | ||||
| @@ -24,11 +28,12 @@ class LineItemsController < ApplicationController | |||||
| # POST /line_items | # POST /line_items | ||||
| # POST /line_items.json | # POST /line_items.json | ||||
| def create | def create | ||||
| @line_item = LineItem.new(line_item_params) | |||||
| product = Product.find(params[:product_id]) | |||||
| @line_item = @cart.line_items.build(product: product) | |||||
| respond_to do |format| | respond_to do |format| | ||||
| if @line_item.save | if @line_item.save | ||||
| format.html { redirect_to @line_item, notice: 'Line item was successfully created.' } | |||||
| format.html { redirect_to @line_item.cart, notice: 'Line item was successfully created.' } | |||||
| format.json { render :show, status: :created, location: @line_item } | format.json { render :show, status: :created, location: @line_item } | ||||
| else | else | ||||
| format.html { render :new } | format.html { render :new } | ||||
| @@ -1,4 +1,9 @@ | |||||
| <p id="notice"><%= notice %></p> | <p id="notice"><%= notice %></p> | ||||
| <%= link_to 'Edit', edit_cart_path(@cart) %> | | |||||
| <%= link_to 'Back', carts_path %> | |||||
| <h2>Your Pragmatic Cart</h2> | |||||
| <ul> | |||||
| <% @cart.line_items.each do |item| %> | |||||
| <li><%= item.product.title %></li> | |||||
| <% end %> | |||||
| </ul> | |||||
| @@ -11,6 +11,7 @@ | |||||
| <%= sanitize(product.description) %> | <%= sanitize(product.description) %> | ||||
| <div class="price_line"> | <div class="price_line"> | ||||
| <span class="price"><%= number_to_currency(product.price) %></span> | <span class="price"><%= number_to_currency(product.price) %></span> | ||||
| <%= button_to 'Add To Cart', line_items_path(product_id: product) %> | |||||
| </div> | </div> | ||||
| </div> | </div> | ||||
| <% end %> | <% end %> | ||||
| @@ -17,10 +17,13 @@ class LineItemsControllerTest < ActionDispatch::IntegrationTest | |||||
| test "should create line_item" do | test "should create line_item" do | ||||
| assert_difference('LineItem.count') do | assert_difference('LineItem.count') do | ||||
| post line_items_url, params: { line_item: { cart_id: @line_item.cart_id, product_id: @line_item.product_id } } | |||||
| post line_items_url, params: { product_id: products(:ruby).id } | |||||
| end | end | ||||
| assert_redirected_to line_item_url(LineItem.last) | |||||
| follow_redirect! | |||||
| assert_select 'h2', 'Your Pragmatic Cart' | |||||
| assert_select 'li', 'Programming Ruby 1.9' | |||||
| end | end | ||||
| test "should show line_item" do | test "should show line_item" do | ||||