我有一個簡單的形式,需要一些字段,並將它們添加到數據庫。表單下方是一個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框架的理解。
謝謝!我試圖模仿腳手架,因爲我想讓我的驗證工作。這只是一個簡單的應用程序,所以現在我不在乎安寧。因此,如果我從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