| @@ -18,6 +18,7 @@ gem 'coffee-rails', '~> 4.2' | |||||
| # Use jquery as the JavaScript library | # Use jquery as the JavaScript library | ||||
| gem 'jquery-rails' | gem 'jquery-rails' | ||||
| gem 'jquery-ui-rails' | |||||
| # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks | # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks | ||||
| gem 'turbolinks', '~> 5' | gem 'turbolinks', '~> 5' | ||||
| # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder | # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder | ||||
| @@ -63,6 +63,8 @@ GEM | |||||
| rails-dom-testing (>= 1, < 3) | rails-dom-testing (>= 1, < 3) | ||||
| railties (>= 4.2.0) | railties (>= 4.2.0) | ||||
| thor (>= 0.14, < 2.0) | thor (>= 0.14, < 2.0) | ||||
| jquery-ui-rails (5.0.5) | |||||
| railties (>= 3.2.16) | |||||
| listen (3.0.8) | listen (3.0.8) | ||||
| rb-fsevent (~> 0.9, >= 0.9.4) | rb-fsevent (~> 0.9, >= 0.9.4) | ||||
| rb-inotify (~> 0.9, >= 0.9.7) | rb-inotify (~> 0.9, >= 0.9.7) | ||||
| @@ -158,6 +160,7 @@ DEPENDENCIES | |||||
| coffee-rails (~> 4.2) | coffee-rails (~> 4.2) | ||||
| jbuilder (~> 2.5) | jbuilder (~> 2.5) | ||||
| jquery-rails | jquery-rails | ||||
| jquery-ui-rails | |||||
| listen (~> 3.0.5) | listen (~> 3.0.5) | ||||
| puma (~> 3.0) | puma (~> 3.0) | ||||
| rails (~> 5.0.0, >= 5.0.0.1) | rails (~> 5.0.0, >= 5.0.0.1) | ||||
| @@ -11,6 +11,7 @@ | |||||
| // about supported directives. | // about supported directives. | ||||
| // | // | ||||
| //= require jquery | //= require jquery | ||||
| //= require jquery-ui/effect-blind | |||||
| //= require jquery_ujs | //= require jquery_ujs | ||||
| //= require turbolinks | //= require turbolinks | ||||
| //= require_tree . | //= require_tree . | ||||
| @@ -34,7 +34,7 @@ class LineItemsController < ApplicationController | |||||
| respond_to do |format| | respond_to do |format| | ||||
| if @line_item.save | if @line_item.save | ||||
| format.html { redirect_to @line_item.cart } | format.html { redirect_to @line_item.cart } | ||||
| format.js | |||||
| format.js { @current_item = @line_item } | |||||
| 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,8 @@ | |||||
| <tr> | |||||
| <% if line_item == @current_item %> | |||||
| <tr id="current_item"> | |||||
| <% else %> | |||||
| <tr> | |||||
| <% end %> | |||||
| <td><%= line_item.quantity %> ×</td> | <td><%= line_item.quantity %> ×</td> | ||||
| <td><%= line_item.product.title %></td> | <td><%= line_item.product.title %></td> | ||||
| <td class="item_price"><%= number_to_currency(line_item.total_price) %></td> | <td class="item_price"><%= number_to_currency(line_item.total_price) %></td> | ||||
| @@ -1 +1,4 @@ | |||||
| $('#cart').html("<%=j render(@cart) %>") | $('#cart').html("<%=j render(@cart) %>") | ||||
| $('#current_item').css({'background-color':'#88ff88'}) | |||||
| .animate({'background-color':'#114411'}, 1000) | |||||
| @@ -26,6 +26,17 @@ class LineItemsControllerTest < ActionDispatch::IntegrationTest | |||||
| assert_select 'td', 'Programming Ruby 1.9' | assert_select 'td', 'Programming Ruby 1.9' | ||||
| end | end | ||||
| test "should create line_item via ajax" do | |||||
| assert_difference('LineItem.count') do | |||||
| post line_items_url, params: { product_id: products(:ruby).id }, xhr: true | |||||
| end | |||||
| assert_response :success | |||||
| assert_select_jquery :html, '#cart' do | |||||
| assert_select 'tr#current_item td', /Programming Ruby 1.9/ | |||||
| end | |||||
| end | |||||
| test "should show line_item" do | test "should show line_item" do | ||||
| get line_item_url(@line_item) | get line_item_url(@line_item) | ||||
| assert_response :success | assert_response :success | ||||