I have a simple form that takes some fields and adds them to the database. Underneath the form is a html table which displays all the products currently in the DB.
Controller: pages
, action: product
#@product is for form capture
#@prods is for displaying list of all products
def product
@product = Product.new(params[:page])
respond_to do |format|
if @product.save
@prods = Product.find(:all)
flash[:product] = "Product #{params[:pname]} saved successfully"
else
format.html { render :controller => "pages", :action => "product" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
@prods = Product.find(:all)
flash[:product] = "Currently there are #{@prods.size} products"
end
View:
<!--show form-->
<% form_for(@product) do |p| %>
<%= p.error_messages %>
<tr><td><%=p.label 'Product*'%></td>
<td><%=p.text_field :prod_name%></td>
</tr>
<tr><td><%=p.label 'Product Price*'%></td>
<td><%=p.text_field :prod_orig_price%></td>
</tr>
<tr>
<td><%=p.label 'Product Discount'%></td>
<td><%=p.text_field :prod_dis_per%></td>
</tr>
<tr>
<td><%=p.label 'Product Promo Code'%></td>
<td><%=p.text_field :prod_promo%></td>
</tr>
<tr align="center"><td colspan="4"><%= submit_tag "Add Product" %></td>
</tr>
</table>
<%end%>
<!--show table-->
<%if @prods != nil%>
<tr>
<th>Product Name</th>
<th>Product Price</th>
<th>Product Discount</th>
<th>Product Promocode</th>
</tr>
<% for p in @prods%>
<tr>
<td><%=p.prod_name%></td>
<td><%=p.prod_orig_price%></td>
<td><%=p.prod_dis_price%></td>
<td><%=p.prod_promo%></td>
</tr>
<%end%>
<%end%>
The first time around the page is loaded fine. I see the form and I see the table with populated products. However, when I try to leave all fields empty so I can see validation is working then I see a message
uninitialized constant ProductsController
I have
map.resources :products
inside my routes.rb
My rails version is 2.3.5.
I've been at this for a while. I am sure I'm missing something basic because of my lack of understating of rails framework.