Beginning ruby session problem / how do I rollback with git?


#1

Hi,

Rails 2.3.2

I’m following along with the examples in AWDWR (3rd), and around about
p. 103, where you are working with a session table you created, I
decided to free lance a bit. At some point, I tried to “clear” the
session by doing the following:

class StoreController < ApplicationController

private
def find_cart
#session[:cart] ||= Cart.new
session[:cart] = Cart.new #<------********
end

I figured that would “erase” the old cart, and let me continue with a
new, empty cart. However, everything is completely messed up now, and
when I try to access a “store page” (which lists the products for sale)
using the following url:

http://localhost:3000/store

I get an error page that says:

We’re sorry but something went wrong.
We’ve been notified about this issue and
we will take a look at it shortly.

As a fix, I tried rolling back the migration that created the session
table:

rake db:rollback

and then doing:

db:migrate

but I get that same error page. So I’m stuck. So much for free
lancing. Is there some obvious way I can get rid of that error page and
get sessions working again.

Alternatively, I have git installed and I committed my changes at the
end of the last chapter(p. 96). But I’ve read several git tutorials,
and I can’t figure out how to rollback to a previous version of my code.

Thanks.


#2

7stud – wrote:

Hi,

Rails 2.3.2

However, everything is completely messed up now, and
when I try to access a “store page” (which lists the products for sale)
using the following url:

http://localhost:3000/store

I get an error page that says:

We’re sorry but something went wrong.
We’ve been notified about this issue and
we will take a look at it shortly.

Sorry, I can access the store page. It’s when I click a button that
calls an add_to_cart action that I get that error message. This is the
url after I click on the add_to_cart button:

http://localhost:3000/store/add_to_cart/5


#3

7stud – wrote:

Sorry, I can access the store page. It’s when I click a button that
calls an add_to_cart action that I get that error message. This is the
url after I click on the add_to_cart button:

http://localhost:3000/store/add_to_cart/5

Here is the add_to_cart action:

class StoreController < ApplicationController
def index
@products = Product.find_products_for_sale

now = Time.now
@date = now.strftime("%m/%d/%Y")
@time = now.strftime("%I:%M%p")

end

def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)

end

private
def find_cart
session[:cart] ||= Cart.new
end
end


#4

I figured out how to rollback with git from this website:

http://www-cs-students.stanford.edu/~blynn/gitmagic/ch02.html#_advanced_undo_redo

$ git log
commit 1964b4f78d82da730ccf5fc6d1ba7d1e1b5ae266
Author: me
Date: Wed Apr 22 18:57:48 2009 -0600

End of chapter 7, p. 96

commit a262fdcd503766cae2c5643923180480151ff669
Author: me
Date: Tue Apr 21 03:28:49 2009 -0600

Initial import

/depot$ git reset --hard 1964b4f78d82da730ccf5fc6d1ba7d1e1b5ae266
HEAD is now at 1964b4f End of chapter 7, p. 96
$

However, apparently doing a rollback doesn’t delete the new files you
created since the last commit. So, I also deleted the migration file
for creating the sessions table.

Then I started over with the steps at the beginning of the chapter.
After adding all the necessary code, I clicked on the add to cart
button, and I get the same error message:

http://localhost:3000/store/add_to_cart/5

We’re sorry, but something went wrong.
We’ve been notified about this issue and we’ll take a look at it
shortly.

So it seems like there is something wrong with my database.


#5

7stud – wrote:

private
def find_cart
session[:cart] ||= Cart.new
end

If I change that method to:

def find_cart
Cart.new
end

then clicking on an “add to cart” button no longer shows me the error
message. I get a view that lists what was added to the cart. However,
without sessions the view just shows the current item that is being
added.

As soon as I add back the line that mentions sessions:

private
def find_cart
session[:cart] ||= Cart.new
end

I get the error message.

I tried rolling back the migration again:

rake db:rollback

then deleting the migration file:

/depot/db/migrate$ 20090423153751_create_sessions.rb

then recreating the migration file:

/depot$ rake db:sessions:create

and recreating the table:

/depot$ rake db:migrate

then restarting the server. But that did not solve my problem.


#6

7stud – wrote:

So it seems like there is something wrong with my database.

I tried rolling back my migrations to the beginning:

/depot$ rake db:migrate VERSION=0
(in /Users/me/2testing/dir1/rails/depot)
== CreateSessions: reverting

– drop_table(:sessions)
-> 0.0014s
== CreateSessions: reverted (0.0016s)

== AddTestData: reverting

== AddTestData: reverted (0.0034s)

== AddPriceToProduct: reverting

– remove_column(:products, :price)
-> 0.0263s
== AddPriceToProduct: reverted (0.0265s)

== CreateProducts: reverting

– drop_table(:products)
-> 0.0010s
== CreateProducts: reverted (0.0012s)

which left me with this:

/depot$ sqlite3 db/development.sqlite3

SQLite version 3.6.13
Enter “.help” for instructions
Enter SQL statements terminated with a “;”

sqlite> .mode line
sqlite> select * from sqlite_master;

type = table
name = schema_migrations

tbl_name = schema_migrations
rootpage = 2
sql = CREATE TABLE “schema_migrations” (“version” varchar(255) NOT
NULL)

type = index
name = unique_schema_migrations

tbl_name = schema_migrations
rootpage = 3
sql = CREATE UNIQUE INDEX “unique_schema_migrations” ON
“schema_migrations” (“version”)

type = table
name = sqlite_sequence

tbl_name = sqlite_sequence
rootpage = 5
sql = CREATE TABLE sqlite_sequence(name,seq)

Then I recreated all the tables:

/depot$ rake db:migrate
(in /Users/me/2testing/dir1/rails/depot)
== CreateProducts: migrating

– create_table(:products)
-> 0.0019s
== CreateProducts: migrated (0.0021s)

== AddPriceToProduct: migrating

– add_column(:products, :price, :decimal, {:precision=>8, :scale=>2,
:default=>0})
-> 0.0118s
== AddPriceToProduct: migrated (0.0122s)

== AddTestData: migrating

== AddTestData: migrated (0.0127s)

== CreateSessions: migrating

– create_table(:sessions)
-> 0.0015s
– add_index(:sessions, :session_id)
-> 0.0004s
– add_index(:sessions, :updated_at)
-> 0.0004s
== CreateSessions: migrated (0.0027s)

Then If I use this version of the find_cart method in the store
controller:

class StoreController < ApplicationController
def index
@products = Product.find_products_for_sale
now = Time.now
@date = now.strftime("%m/%d/%Y")
@time = now.strftime("%I:%M%p")
end

def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
end

#***********************
private
def find_cart
Cart.new
end
#***********************
end

when I click on my “add to cart” button, a view displays the current
item. However, if I try to use sessions:

#***********************
private
def find_cart
session[:cart] ||= Cart.new
end
#***********************

then I get the error message. The only thing I haven’t tried is to
delete my whole project and start over.