在views/products/edit.html.erb
我使用:
<%= form_for(:product, :url => {:action => 'update', :id => @product.id}) do |f| %>
生成:
<form method="post" action="/aircons/8" accept-charset="UTF-8">
我收到以下错误:
The action '8' could not be found for ProductsController
尝试使用 更新产品时id=8
。
我认为表单的方法应该是put
. 是对的吗 ?我应该如何解决这个问题?
一些控制器代码:
def edit
@product = Product.find(params[:id])
end
def update
update_params_with_new_values(params)
@product = Product.find(params[:id])
if @product.update_attributes(params[:product])
flash[:notice] = "Product updated successfully."
redirect_to(:product => 'index')
else
render('edit')
end
end
def update_params_with_new_values(params)
params[:product][:shop_id] = Shop.create(:name => params[:new_shop]).id if params[:product][:shop_id] == "new_shop"
params[:product][:brand_id] = Brand.create(:name => params[:new_brand]).id if params[:product][:brand_id] == "new_brand"
end
routes.rb
仅包含以下两行:
root :to => "products#index"
resources :products