9.3 Iteration D3: Adding a button 9.1 Iteration D1: Finding a Cart
22 (tests|runs), 44 assertions, 0 failures, 0 errors. <0> expected to be >= <1>. Traceback: /home/rubys/git/awdwr/edition4/checkdepot.rb:36:in `assert_test_summary' /home/rubys/git/awdwr/edition4/checkdepot.rb:136:in `block in <class:DepotTest>'
Create line item which connects products to carts'
Create the model object.
rails generate scaffold LineItem product:references cart:belongs_to
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from rescue in <class:Exception> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/integration/cruby.rb:37)
invoke active_record
create db/migrate/20150330104508_create_line_items.rb
create app/models/line_item.rb
invoke test_unit
create test/models/line_item_test.rb
create test/fixtures/line_items.yml
invoke resource_route
route resources :line_items
invoke scaffold_controller
create app/controllers/line_items_controller.rb
invoke erb
create app/views/line_items
create app/views/line_items/index.html.erb
create app/views/line_items/edit.html.erb
create app/views/line_items/show.html.erb
create app/views/line_items/new.html.erb
create app/views/line_items/_form.html.erb
invoke test_unit
create test/controllers/line_items_controller_test.rb
invoke helper
create app/helpers/line_items_helper.rb
invoke test_unit
invoke jbuilder
create app/views/line_items/index.json.jbuilder
create app/views/line_items/show.json.jbuilder
invoke assets
invoke coffee
create app/assets/javascripts/line_items.coffee
invoke scss
create app/assets/stylesheets/line_items.scss
invoke scss
identical app/assets/stylesheets/scaffolds.scss
rake db:migrate
mv 20150330104508_create_line_items.rb 20150330000003_create_line_items.rb
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from rescue in <class:Exception> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/integration/cruby.rb:37)
== 20150330000003 CreateLineItems: migrating ==================================
-- create_table(:line_items)
-> 0.0041s
== 20150330000003 CreateLineItems: migrated (0.0041s) =========================
Cart has many line items.
edit app/models/cart.rb
class Cart < ActiveRecord::Base
has_many :line_items, dependent: :destroy
end
Product has many line items.
edit app/models/product.rb
class Product < ActiveRecord::Base
has_many :line_items
before_destroy :ensure_not_referenced_by_any_line_item
#...
private
# ensure that there are no line items referencing this product
def ensure_not_referenced_by_any_line_item
if line_items.empty?
return true
else
errors.add(:base, 'Line Items present')
return false
end
end
end
Line item belongs to both Cart and Product (But slightly more to the Cart). Also provide convenient access to the total price of the line item
edit app/models/line_item.rb
class LineItem < ActiveRecord::Base
belongs_to :product
belongs_to :cart
end
rake test:controllers
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from rescue in <class:Exception> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/integration/cruby.rb:37)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from block in <top (required)> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/extensions.rb:18)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /home/rubys/.rvm/gems/ruby-head-n50123/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from rescue in <class:Exception> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/integration/cruby.rb:37)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from block in <top (required)> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/extensions.rb:18)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /home/rubys/.rvm/gems/ruby-head-n50123/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
Run options: --seed 9137
# Running:
DEPRECATION WARNING: before_filter is deprecated and will be removed in Rails 5.1. Use before_action instead. (called from block (3 levels) in <class:Engine> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/turbolinks-2.5.3/lib/turbolinks.rb:14)
DEPRECATION WARNING: after_filter is deprecated and will be removed in Rails 5.1. Use after_action instead. (called from block (3 levels) in <class:Engine> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/turbolinks-2.5.3/lib/turbolinks.rb:15)
............F.......EE
Finished in 0.852015s, 25.8211 runs/s, 43.4265 assertions/s.
1) Failure:
LineItemsControllerTest#test_should_create_line_item [/home/rubys/git/awdwr/edition4/work-220/depot/test/controllers/line_items_controller_test.rb:20]:
"LineItem.count" didn't change by 1.
Expected: 3
Actual: 2
2) Error:
ProductsControllerTest#test_should_update_product:
ActionController::UrlGenerationError: No route matches {:action=>"update", :controller=>"products", :description=>"Wibbles are fun!", :image_url=>"lorem.jpg", :price=>"19.95", :title=>"Lorem Ipsum"}
test/controllers/products_controller_test.rb:57:in `block in <class:ProductsControllerTest>'
3) Error:
ProductsControllerTest#test_should_create_product:
ActionController::ParameterMissing: param is missing or the value is empty: product
app/controllers/products_controller.rb:79:in `product_params'
app/controllers/products_controller.rb:27:in `create'
test/controllers/products_controller_test.rb:34:in `block (2 levels) in <class:ProductsControllerTest>'
test/controllers/products_controller_test.rb:32:in `block in <class:ProductsControllerTest>'
22 runs, 37 assertions, 1 failures, 2 errors, 0 skips
Failed tests:
bin/rails test /home/rubys/git/awdwr/edition4/work-220/depot/test/controllers/line_items_controller_test.rb:19
bin/rails test /home/rubys/git/awdwr/edition4/work-220/depot/test/controllers/products_controller_test.rb:55
bin/rails test /home/rubys/git/awdwr/edition4/work-220/depot/test/controllers/products_controller_test.rb:31
9.3 Iteration D3: Adding a button 9.1 Iteration D1: Finding a Cart