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.
There’s a lot going on here, however to fix your immediate problem you should do this:
indexaction in the products controllerdef productto your newdef indexaction./pages/products.html.erbto/products/index.html.erb.I’m going to edit this to explain why, but for now this should get you started.
The why!
When you define a restful resource in your
routes.rb, for example by writingmap.resources :products, rails expects you to write your controllers in a restful fashion. What this means to you is that if you want a products resource, you’re going to need need aProductsControllerwith the following actions (you only have to implement the ones you need):index, show, new, create, edit, update, delete. This should explain why your code wasn’t working.I’m not sure what the contents of your
routes.rbis, but I’m guessing you have your default route still defined, which would explain why you could browse to/pages/products.When you use
form_for(@product)rails will generate<form action="/products" method="post">, which will map to yournewaction.Having said all this I suggest you do the following:
/newpage for products which displays a form, you can always<%= render :partial => 'form' %>in your other actions to share the new product functionality. This will help with testing and make your code easier to maintain.Rails can have a pretty steep learning curve but there’s a lot of literature out there to help you. I hope this answer points you in the right direction and gives you something to think about.