2010-06-16 66 views
0

我该如何传入ID?PUT-form更新行,但我找不到id。它在哪里?

错误:

Couldn't find Product without an ID

形式:

<% form_for :product, @product, :url => { :action => :update } do |f| %> 
    <%= f.error_messages %> 

    <p> 
    <%= f.label :names %><br /> 
    <%= f.text_field :names %> 
    </p> 
    <p> 
    <%= f.submit 'Update' %> 
    </p> 
<% end %> 

控制器(用于/产品/编辑/ 1视图):

def edit 
    @product = Product.find(params[:id]) 
end 

控制器(改变分贝):

def update 
    @product = Product.find(params[:id]) 

    respond_to do |format| 
     if @product.update_attributes(params[:product]) 
     format.html { redirect_to(@product, :notice => 'Product was successfully updated.') } 
     format.xml { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @product.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

回答

2

尝试删除:从的form_for声明

编辑产品:尝试删除URL选项也。问题是您发布的URL没有id属性。

+0

没有工作,不幸的是 – montooner 2010-06-16 22:20:42

+0

这导致t o“没有任何操作回应1.操作:创建,编辑,索引,新建,显示和更新”问题“ – montooner 2010-06-16 22:30:10

+0

尝试在您的URL散列中添加:id => @ product.id。 此外,你是否在你的路线中使用map.resources? – Maz 2010-06-16 22:32:50

2

这种形式的标签应该是必须的:

<% form_for @product do |f| %>

确保在你的config/routes.rb中有这样一行:

map.resources :products

对于额外的信用,可以简化装载@product在你的控制器上用before_filter

class ProductsController < ApplicationController 
    before_filter :load_product, :only => [:show, :edit, :update, :destroy] 
    def load_product 
    @product = Product.find(params[:id]) 
    end 
end 
+0

感谢额外的功劳!棒极了。一直在寻找。 – montooner 2010-06-16 23:04:05

相关问题