编辑操作不适用于模型实例?

时间:2014-07-07 20:21:18

标签: ruby-on-rails ruby

在我的Rails应用程序中,我正在尝试创建一个函数来加载表单以更新模型实例的属性然后保存它,但是当我点击时我遇到了“未定义的方法'编辑'”错误我做的链接去表格。

以下是编辑表格:

<h1>Edit your listing</h1>
  <%= form_for Product.edit, url: {action: "update"} do |f| %>
  <div><%= f.label :name %><br />
    <%= f.text_field :name, :placeholder => "Name yourself" %>
  </div>
  <div><%= f.label :price %><br />
    <%= f.number_field :price, :placeholder => "Name your price" %>
  </div><br />
  <div><%= f.label :description %><br />
    <%= f.text_area :description, :cols => "50", :rows => "10", :placeholder => "Write a few sentences about the item you're listing. Is it in good condition? Are there any accessories included?"%>
   </div>
   <br />
   <%= f.submit "Update listing" %>
<% end %>

错误是说Product.edit未定义,但操作在我的控制器中。他们在这里:

def edit
respond_to do |format|
  format.js
  format.html
end
end

def update
  @product = Product.find(params[:id])
  respond_to do |format|
    if @product.update_attributes(params[:product])
      format.html {render :action => "show"}
    else
      format.html {render :action => "edit"}
    end
end

这是我创建的路线:

get "/products/:id/edit(.:format)" => "products#edit", :as => "edit_item"

最后,应该指向表单的链接:

<h3><%= link_to "Edit listing", edit_item_path, :method => :get %></h3>

知道问题可能是什么?

1 个答案:

答案 0 :(得分:1)

您需要更改:

<%= form_for Product.edit, url: {action: "update"} do

<%= form_for @product do

您的编辑操作:

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