Help with the Depot App

Hi all,

Warning noob question!

I’m having an absolute blast toying with the Depot example in Agile Web
Dev. I made a method that looks like this:

def purchase
@local = Product.find(params[:id])
if @local.status == 0
@local.status = 1
flash[:notice] = ‘Product Marked!’
redirect_to :action => ‘list’, :id => @local
elsif @local.status == 1
flash[:notice] = ‘Product Already Marked!’
redirect_to :action => ‘list’, :id => @local
end
end

All this is supposed to do is to mark a “1” to a field “status” in the
products table to signify that the product has been purchased, I had it
working a week ago but I overwrote the file accidentally and I don’t
understand what I did. As it stands right now it dumps me to a ‘Missing
Template’ page. I appreciate ALL input.

Both the condition is not satisfied so u r getting missing template
error.
@local.status will have the string value. You are comparing the string
value with integer.

try like this @local.status.to_i == 0 .

On Nov 27, 2007, at 9:52 PM, Cosmas Atha wrote:

@local = Product.find(params[:id])
All this is supposed to do is to mark a “1” to a field “status” in the
products table to signify that the product has been purchased, I had
it
working a week ago but I overwrote the file accidentally and I don’t
understand what I did. As it stands right now it dumps me to a
‘Missing
Template’ page. I appreciate ALL input.

Do you have a list.rhtml file in views/ (name of your controller)?
Can you post some of the actual error message?

(By the way, if list is just a list then you probably don’t need
the :id => @local in the redirect, as it won’t be used for anything.
It doesn’t hurt anything though.)

On Nov 27, 2007, at 10:28 PM, San wrote:

Both the condition is not satisfied so u r getting missing template
error.
@local.status will have the string value. You are comparing the string
value with integer.

It wouldn’t necessarily be a string. It’s coming from the model. But
you’re right, if it is a string rails will be looking for purchase.rhtml

George B. wrote:

Do you have a list.rhtml file in views/ (name of your controller)?
Can you post some of the actual error message?

(By the way, if list is just a list then you probably don’t need
the :id => @local in the redirect, as it won’t be used for anything.
It doesn’t hurt anything though.)

Yea thats not supposed to look like that actually…

def purchase
@local = Product.find(params[:id])
if @local.status.to_i == 0
@local.status = 1
flash[:notice] = ‘Product Marked!’
redirect_to :action => ‘show’, :id => @local
elsif @local.status == 1
flash[:notice] = ‘Product Already Marked!’
redirect_to :action => ‘show’, :id => @local
end
end

The show method displays the individual product. If I click my purchase
button on the page… it redirects and says ‘Product Marked’ but when I
look over in MySql… the value didn’t change.

On Nov 27, 2007, at 10:44 PM, Cosmas Atha wrote:

 redirect_to :action => 'show', :id => @local

end
end

Is status a string or an integer? If it’s a string just do
if @local.status == “0”

The show method displays the individual product. If I click my
purchase
button on the page… it redirects and says ‘Product Marked’ but when I
look over in MySql… the value didn’t change.

Are you saving it?
Either a @local.save, or probably better is
@local.update_attribute(:status, “1”)

The advantage of the update_attribute is that it will only update that
one column instead of rewriting the entire row.

Assuming status is a string, I’d write it like this:

def purchase
@local = Product.find(params[:id])
if @local.status == “0”
@local.update_attribute(:status, “1”)
flash[:notice] = ‘Product Marked!’
elsif @local.status == “1”
flash[:notice] = ‘Product Already Marked!’
else
flash[:notice] = “Status is #{@local.status} for some reason.
This is not good.”
end
redirect_to :action => ‘show’, :id => @local
end

George B. wrote:

On Nov 27, 2007, at 10:44 PM, Cosmas Atha wrote:

 redirect_to :action => 'show', :id => @local

end
end

Is status a string or an integer? If it’s a string just do
if @local.status == “0”

The show method displays the individual product. If I click my
purchase
button on the page… it redirects and says ‘Product Marked’ but when I
look over in MySql… the value didn’t change.

Are you saving it?
Either a @local.save, or probably better is
@local.update_attribute(:status, “1”)

The advantage of the update_attribute is that it will only update that
one column instead of rewriting the entire row.

Assuming status is a string, I’d write it like this:

def purchase
@local = Product.find(params[:id])
if @local.status == “0”
@local.update_attribute(:status, “1”)
flash[:notice] = ‘Product Marked!’
elsif @local.status == “1”
flash[:notice] = ‘Product Already Marked!’
else
flash[:notice] = “Status is #{@local.status} for some reason.
This is not good.”
end
redirect_to :action => ‘show’, :id => @local
end

Ok… says:

Status is for some reason. This is not good.

Status was an INT, I just changed it to a VARCHAR(1) and got the same
result. George, your code looks solid… I can’t imagine whats going on.
It seems like its not even testing status. Would it help if I posted the
dev log?

On Nov 27, 2007, at 11:44 PM, Cosmas Atha wrote:

Ok… says:

Status is for some reason. This is not good.

Status was an INT, I just changed it to a VARCHAR(1) and got the same
result. George, your code looks solid… I can’t imagine whats going
on.
It seems like its not even testing status. Would it help if I posted
the
dev log?

If you changed to a String it sounds like status is empty or nil.
If status was an int, just remove the quotes from around the digits
and it will work.

Put this in temporarily:

def purchase
@local = Product.find(params[:id])
if @local.status.blank?
flash[:notice] = “Status is nil”
elsif @local.status.empty?
flash[:notice] = “Status is an empty string”
elsif @local.status == “0”
@local.update_attribute(:status, “1”)
flash[:notice] = ‘Product Marked!’
elsif @local.status == “1”
flash[:notice] = ‘Product Already Marked!’
else
flash[:notice] = “Status is #{@local.status} for some reason.
This is not good.”
end
redirect_to :action => ‘show’, :id => @local
end

On Nov 28, 2007, at 12:20 AM, Cosmas Atha wrote:

I changed status back to an integer and did what you said. Reported
back: “Status is nil” Even though I set 0 for the default value. I
also
manually put in a 1 for the field and it said the same thing.

Are you sure you’re looking at the right record?

Change that notice to
flash[:notice] = “Status is nil for product #{@local.description}, id:
#{@local.id.to_s
}”

“description” may not be right, of course. Use whatever the column is
for the product name.

Put this in temporarily:

def purchase
@local = Product.find(params[:id])
if @local.status.blank?
flash[:notice] = “Status is nil”
elsif @local.status.empty?
flash[:notice] = “Status is an empty string”
elsif @local.status == “0”
@local.update_attribute(:status, “1”)
flash[:notice] = ‘Product Marked!’
elsif @local.status == “1”
flash[:notice] = ‘Product Already Marked!’
else
flash[:notice] = “Status is #{@local.status} for some reason.
This is not good.”
end
redirect_to :action => ‘show’, :id => @local
end

I changed status back to an integer and did what you said. Reported
back: “Status is nil” Even though I set 0 for the default value. I also
manually put in a 1 for the field and it said the same thing.

George B. wrote:

On Nov 28, 2007, at 12:20 AM, Cosmas Atha wrote:

I changed status back to an integer and did what you said. Reported
back: “Status is nil” Even though I set 0 for the default value. I
also
manually put in a 1 for the field and it said the same thing.

Are you sure you’re looking at the right record?

Change that notice to
flash[:notice] = “Status is nil for product #{@local.description}, id:
#{@local.id.to_s
}”

“description” may not be right, of course. Use whatever the column is
for the product name.

FIXED!
I feel really stupid though… I had:
class Product < ActiveRecord::Base

attr_accessor :status

when I needed…

attr_accessible :status

Isn’t it always something stupid?..thanks for all the help.

On Nov 28, 2007, at 12:58 AM, Cosmas Atha wrote:

FIXED!

Glad to hear it.

I feel really stupid though… I had:
class Product < ActiveRecord::Base

attr_accessor :status

I did the same thing once. I should have remembered.

when I needed…

attr_accessible :status

You don’t need anything. Rails takes care of that for you. And as
you’ve seen, adding the other kind hides the original.
And if you do use attr_accessible you hide any attributes you don’t
explicitly name.

http://railsmanual.com/class/ActiveRecord::Base/attr_accessible

…thanks for all the help.

My pleasure.