2010-01-27 27 views
1

我有一個簡單的形式,需要一些字段,並將它們添加到數據庫。表單下方是一個html表格,顯示當前在數據庫中的所有產品。需要在軌道上的另一個眼睛 - 驗證問題

控制器:pages,動作: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 

查看:

<!--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%> 

第一次頁面各處是加載罰款。我看到了表格,並且看到了帶有填充產品的表格。然而,當我試圖離開所有領域空的,所以我可以看到驗證工作,然後我看到一條消息 uninitialized constant ProductsController 我有 map.resources :products在我routes.rb 我的rails版本是2.3.5。

我一直在這一段時間。我相信我缺少一些基本的東西,因爲我缺乏對Rails框架的理解。

回答

1

有很多會在這裏,然而,能夠立即解決的問題,你應該這樣做:

  1. 腳本/生成器產品
  2. 創建產品控制器的index行動
  3. 移動你的代碼在def product以上到您的新def index行動。
  4. 將您的視圖從/pages/products.html.erb移至/products/index.html.erb

我要編輯這個來解釋爲什麼,但現在這應該讓你開始。

爲什麼!

當您在您的routes.rb中定義一個restful資源時,例如通過編寫map.resources :products,rails期望您以平穩的方式編寫控制器。這對您意味着什麼,如果您需要產品資源,則需要使用ProductsController並執行以下操作(您只需實施所需的產品):index, show, new, create, edit, update, delete。這應該解釋爲什麼你的代碼不工作。

我不確定你的routes.rb的內容是什麼,但我猜你的默認路由仍然定義,這就可以解釋爲什麼你可以瀏覽到/pages/products

當您使用form_for(@product)導軌將生成<form action="/products" method="post">,它將映射到您的new操作。

說了這麼多,我建議你做到以下幾點:

  1. 閱讀上Rails resources
  2. 重構您的應用程序以獲得更多寧靜,例如爲顯示錶單的產品專門設置了一個專門的/new頁面,您可以在其他操作中始終使用<%= render :partial => 'form' %>來共享新產品功能。這將有助於測試並使您的代碼更易於維護。
  3. 看看Inherited resources插件。它會引導你朝着正確的方向發展,並希望能夠幫你避免編寫大量代碼。
  4. 看看formtastic插件。它將幫助您創建語義正確的表格,減少鍋爐板碼,讓您的生活更輕鬆。
  5. 請閱讀rails partials。這將幫助你收緊你的觀點。

Rails可以有一個非常陡峭的學習曲線,但有很多文獻可以幫助你。我希望這個回答能指出你正確的方向,並給你一些想法。

+0

謝謝!我試圖模仿腳手架,因爲我想讓我的驗證工作。這只是一個簡單的應用程序,所以現在我不在乎安寧。因此,如果我從routes.rb中刪除'map.resources:products'並將'form_for(@product)'更改爲'form_for:product',那麼我不會再收到未初始化的常量錯誤。 關於form_for還有一個問題。當我查看我的表單的來源時。參數名稱爲'products [:prod_name]'在我的控制器中,如果我再次獲得它們,例如'Product.new(:prod_name => params [:products] [:prod_name]'或者它們會以某種方式自動映射。 – Omnipresent 2010-01-27 16:58:24

0

除非你不包括所有相關的代碼,你product方法缺少respond_to塊:

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