g agile web development with rails 4
I need your help.
My Ruby on Rails tutorial(agile web development with rails 4, block g) gives me an error. I am new to Ruby and have no idea how to fix it.
Source :
class CombineItemsInCart < ActiveRecord::Migration
def up
Cart.all.each do |cart|
# how many goods are in the cart?
sums = cart.line_items.group(:product_id).sum(:quantity)
sums.each do |product_id; quantity|
if quantity > 1
# remove lines
cart.line_items.where(:product_id product_id).delete_all
# replace with a one line
item = cart.line_items.build(product_id: product_id)
item.quantity = quantity
item.save!
end
end
end
end
end
The error is:
Migrations are pending; run 'rake db:migrate RAILS_ENV=development' to resolve this issue.
But when I tried to run rake db:migrate ,
I receive this error in terminal:
rake db:migrate RAILS_ENV=development
rake aborted! SyntaxError: /home/yury/Desktop/rails/depot_g/db/migrate/20170401195439_combine_items_in_cart.rb:10: syntax error, unexpected tIDENTIFIER, expecting ')' cart.line_items.where(:product_id product_id).delete_all ^ /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in require' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in
block in require' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in load_dependency' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in
require' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:718:in load_migration' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:714:in
migration' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:70 8:in disable_ddl_transaction' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:1012:in
use_transaction?' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:1004:in ddl_transaction' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:958:in
execute_migration_in_transaction' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:920:in block in migrate' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in
each' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in migrate' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:764:in
up' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:742:in migrate' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:42:in
block (2 levels) in ' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/rake-12.0.0/exe/rake:27:in <top (required)>' /home/yury/.rvm/gems/ruby-2.0.0-p648/bin/ruby_executable_hooks:15:in
<top (required)>' /home/yury/.rvm/gems/ruby-2.0.0-p648/bin/ruby_executable_hooks:15:in
eval' /home/yury/.rvm/gems/ruby-2.0.0-p648/bin/ruby_executable_hooks:15:in `' Tasks: TOP => db:migrate (See full trace by running task with --trace)
System: Ubuntu 16.04 64 bit. I checked the code few times and it is the same as in the book.
So could anyone help me and tell what I am doing wrong? And how can I fix it? Thanks!
As it says " syntax error, unexpected tIDENTIFIER, expecting ')'
" then there's a typo in your code syntax and is in your sums.each
block, change
sums.each do |product_id; quantity|
For
sums.each do |product_id, quantity|
And in your if
statement from this
cart.line_items.where(:product_id product_id)
To this
cart.line_items.where(product_id: product_id)
Check the original code from the book:
# rails40/depot_g/db/migrate/20121130000005_combine_items_in_cart.rb
def up
# replace multiple items for a single product in a cart with a single item
Cart.all.each do |cart|
# count the number of each product in the cart
sums = cart.line_items.group(:product_id).sum(:quantity)
sums.each do |product_id, quantity|
if quantity > 1
# remove individual items
cart.line_items.where(product_id: product_id).delete_all
# replace with a single item
item = cart.line_items.build(product_id: product_id)
item.quantity = quantity
item.save!
end
end
end
end
链接地址: http://www.djcxy.com/p/49950.html
上一篇: iPad Javascript / jQuery touchstart问题
下一篇: 灵活的网页开发与轨道4