开发者

need another set of eyes on rails - validation issue

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>Pro开发者_高级运维duct 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:

  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.


Unless you've not included all the relevant code, your product method is missing a respond_to block:

respond_to do |format|
  format.html { render :controller => "pages", :action => "product" } 
  format.xml  { render :xml => @product.errors, :status => :unprocessable_entity 
end
  • Formats and respond_to
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜