11.4 Iteration F4: Hide an Empty Cart 11.2 Iteration F2: Creating an AJAX-Based Cart
Assign the current item to be the line item in question
edit app/controllers/line_items_controller.rb
def create
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product)
respond_to do |format|
if @line_item.save
format.html { redirect_to store_index_url }
format.js { @current_item = @line_item }
format.json { render :show,
status: :created, location: @line_item }
else
format.html { render :new }
format.json { render json: @line_item.errors,
status: :unprocessable_entity }
end
end
end
Add the id to the row in question
edit app/views/line_items/_line_item.html.erb
<% if line_item == @current_item %>
<tr id="current_item">
<% else %>
<tr>
<% end %>
<td><%= line_item.quantity %>×</td>
<td><%= line_item.product.title %></td>
<td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
</tr>
Animate the background color of that row
edit app/views/line_items/create.js.erb
$('#cart').html("<%=j render(@cart) %>");
$('#current_item').css({'background-color':'#88ff88'}).
animate({'background-color':'#114411'}, 1000);
Make the jquery-ui libraries available to the application
edit Gemfile
# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'jquery-ui-rails'
Install the gem
bundle install --local
Resolving dependencies...
Using rake 12.0.0
Using i18n 0.8.4
Using json 1.8.6
Using minitest 5.3.3
Using thread_safe 0.3.6
Using builder 3.2.3
Using erubis 2.7.0
Using rack 1.5.5
Using mime-types-data 3.2016.0521
Using arel 5.0.1.20140414130214
Using bundler 1.15.1
Using thor 0.19.4
Using hike 1.2.3
Using multi_json 1.12.1
Using tilt 1.4.1
Using sqlite3 1.3.13
Using sass 3.2.19
Using execjs 2.7.0
Using coffee-script-source 1.12.2
Using turbolinks-source 5.0.3
Using rdoc 4.3.0
Using spring 1.7.2
Using tzinfo 1.2.3
Using rack-test 0.6.3
Using mime-types 3.1
Using sprockets 2.12.4
Using uglifier 3.2.0
Using coffee-script 2.4.1
Using turbolinks 5.0.1
Using sdoc 0.4.2
Using activesupport 4.1.16 from source at `/home/rubys/git/rails`
Using mail 2.6.5
Using actionview 4.1.16 from source at `/home/rubys/git/rails`
Using activemodel 4.1.16 from source at `/home/rubys/git/rails`
Using jbuilder 2.6.4
Using actionpack 4.1.16 from source at `/home/rubys/git/rails`
Using activerecord 4.1.16 from source at `/home/rubys/git/rails`
Using actionmailer 4.1.16 from source at `/home/rubys/git/rails`
Using railties 4.1.16 from source at `/home/rubys/git/rails`
Using sprockets-rails 2.3.3
Using coffee-rails 4.0.1
Using jquery-rails 3.1.4
Using jquery-ui-rails 6.0.1
Using rails 4.1.16 from source at `/home/rubys/git/rails`
Using sass-rails 4.0.5
Bundle complete! 11 Gemfile dependencies, 45 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
Pull in the blind effect from the jquery-ui libraries
edit app/assets/javascripts/application.js
// This is a manifest file that'll be compiled into application.js, which will
// include all the files listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts,
// vendor/assets/javascripts, or vendor/assets/javascripts of plugins, if any,
// can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at
// the bottom of the compiled file.
//
// Read Sprockets README
// (https://github.com/rails/sprockets#sprockets-directives) for details about
// supported directives.
//
//= require jquery
//= require jquery-ui/effects/effect-blind
//= require jquery_ujs
//= require turbolinks
//= require_tree .
Add an XHR test.
edit test/controllers/line_items_controller_test.rb
test "should create line_item via ajax" do
assert_difference('LineItem.count') do
xhr :post, :create, product_id: products(:ruby).id
end
assert_response :success
assert_select_jquery :html, '#cart' do
assert_select 'tr#current_item td', /Programming Ruby 1.9/
end
end
rake test
Run options: --seed 54388
# Running:
...............................
Finished in 0.268300s, 115.5424 runs/s, 290.7195 assertions/s.
31 runs, 78 assertions, 0 failures, 0 errors, 0 skips
11.4 Iteration F4: Hide an Empty Cart 11.2 Iteration F2: Creating an AJAX-Based Cart