Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 567703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:06:41+00:00 2026-05-13T13:06:41+00:00

I have a simple form that takes some fields and adds them to the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:06:41+00:00Added an answer on May 13, 2026 at 1:06 pm

    There’s a lot going on here, however to fix your immediate problem you should do this:

    1. script/generate controller products
    2. create an index action in the products controller
    3. move your code above in def product to your new def index action.
    4. move your view from /pages/products.html.erb to /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 writing map.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 a ProductsController with 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.rb is, 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 your new action.

    Having said all this I suggest you do the following:

    1. Read up on Rails resources.
    2. Refactor your application to be more restful, for example have a dedicate /new page 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.
    3. Take a look at the Inherited resources plugin. It will guide you in the right direction and hopefully save you from writing a lot of code.
    4. Take a look at the formtastic plugin. It will help you create semantically correct forms, cut down on your boiler plate code and make your life easier.
    5. Read up on rails partials. This will help you tighten up your views.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are some HTML based games (ie bootleggers.us) that have a simple login form
I have a site that has a simple HTML button in a form. All
I have a simple form with some plain html input like bellow using ASP.NET
Say I want to have a simple web app that takes some user input,
I have a simple .NET 2.0 windows form app that runs off of a
I'm writing a simple app that's going to have a tiny form sitting in
I have a relatively simple form which asks a variety of questions. One of
I have a simple form on a view page, implemented as a user control,
Say I have this simple form: class ContactForm(forms.Form): first_name = forms.CharField(required=True) last_name = forms.CharField(required=True)
I'm making my first foray into WPF - I have a simple form with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.