split('.').map(&:to_i)
params[:product][:price] = v[0]*100 + v[1]
end
end
This helper would reformat the price into cents if a price parameter was submitted. The
private in the beginning says that this method should not be available as an action on the
controller.
Next you need to change the create method to handle the new price:
def create
intern_price
@product = Product.new(params[:product])
if @product.save
flash[:notice] = 'Product was successfully created.'
redirect_to :action => 'list'
else
@product_types = ProductType.find(:all)
@product_categories = ProductCategory.find(:all)
render :action => 'new'
end
end
The other thing you added was @product_types and @product_categories, in case something
goes wrong. This lets you see the original page again, with error messages attached. You
need to do the same thing with the update method:
def update
@product = Product.find(params[:id])
intern_price
if @product.update_attributes(params[:product])
flash[:notice] = 'Product was successfully updated.'
redirect_to :action => 'show', :id => @product
else
@product_types = ProductType.find(:all)
@product_categories = ProductCategory.find(:all)
render :action => 'edit'
end
end
CHAPTER 4 ?– STORE ADMINISTRATION 53
Now you can go ahead and create a product or two. You don??™t need to restart the web
server either. If everything works correctly, you should also be able to see your new products
in the list. You can edit and destroy products without trouble, too.
Pages:
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128