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.
@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.)
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
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…
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.
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
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?
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
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.
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.
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.
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.