1

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
4

2 回答 2

6

你为什么不直接使用:

<%= form_for @product do |f| %>

?

如果它不起作用,请将您的路线添加到问题中。

于 2010-12-14T12:55:35.290 回答
1

尝试使用这个

<% form_for @product %>
#code goes here
<% end %>

你不需要做你正在尝试的所有事情。如果您Product使用脚手架机制创建了这个模型,您必须在config/routes.rb文件中有它的条目,这将为您提供一个路径变量,如下所示

GET     /products/:id/edit      edit    return an HTML form for editing a photo
PUT     /products/:id   update  update a specific photo

您可以获取编辑路径以edit_product_path获取有关此的更多信息,请查看此

希望你现在能更好地理解它。

于 2010-12-14T12:50:58.080 回答