From e2a4ad7e165ebc5e0aa1e513210db63aaccbf4e8 Mon Sep 17 00:00:00 2001 From: Nils Dittberner Date: Wed, 16 Nov 2016 09:35:26 +0100 Subject: [PATCH] Iteration D3. --- app/assets/stylesheets/store.scss | 4 ++++ app/controllers/line_items_controller.rb | 9 +++++++-- app/views/carts/show.html.erb | 9 +++++++-- app/views/store/index.html.erb | 1 + test/controllers/line_items_controller_test.rb | 7 +++++-- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/assets/stylesheets/store.scss b/app/assets/stylesheets/store.scss index 42f062a..e50e369 100644 --- a/app/assets/stylesheets/store.scss +++ b/app/assets/stylesheets/store.scss @@ -37,6 +37,10 @@ margin-left: 100px; margin-top: 0.5em; margin-bottom: 0.8em; + + form, div { + display: inline; + } } .price { diff --git a/app/controllers/line_items_controller.rb b/app/controllers/line_items_controller.rb index 1341422..b41c2f1 100644 --- a/app/controllers/line_items_controller.rb +++ b/app/controllers/line_items_controller.rb @@ -1,6 +1,10 @@ class LineItemsController < ApplicationController + include CurrentCart + before_action :set_cart, only: [:create] before_action :set_line_item, only: [:show, :edit, :update, :destroy] + # TODO: Create counter from play time. + # GET /line_items # GET /line_items.json def index @@ -24,11 +28,12 @@ class LineItemsController < ApplicationController # POST /line_items # POST /line_items.json 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| 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 } else format.html { render :new } diff --git a/app/views/carts/show.html.erb b/app/views/carts/show.html.erb index 32f7a23..a3d8b09 100644 --- a/app/views/carts/show.html.erb +++ b/app/views/carts/show.html.erb @@ -1,4 +1,9 @@

<%= notice %>

-<%= link_to 'Edit', edit_cart_path(@cart) %> | -<%= link_to 'Back', carts_path %> +

Your Pragmatic Cart

+ + diff --git a/app/views/store/index.html.erb b/app/views/store/index.html.erb index f82b1b5..300f70d 100644 --- a/app/views/store/index.html.erb +++ b/app/views/store/index.html.erb @@ -11,6 +11,7 @@ <%= sanitize(product.description) %>
<%= number_to_currency(product.price) %> + <%= button_to 'Add To Cart', line_items_path(product_id: product) %>
<% end %> diff --git a/test/controllers/line_items_controller_test.rb b/test/controllers/line_items_controller_test.rb index ed2f9c3..2999263 100644 --- a/test/controllers/line_items_controller_test.rb +++ b/test/controllers/line_items_controller_test.rb @@ -17,10 +17,13 @@ class LineItemsControllerTest < ActionDispatch::IntegrationTest test "should create line_item" 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 - assert_redirected_to line_item_url(LineItem.last) + follow_redirect! + + assert_select 'h2', 'Your Pragmatic Cart' + assert_select 'li', 'Programming Ruby 1.9' end test "should show line_item" do