Ruby on Rails:为什么要“编辑”?行动不起作用?

发布于 2024-10-07 16:22:52 字数 1275 浏览 0 评论 0原文

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

In views/products/edit.html.erb I use:

<%= form_for(:product, :url => {:action => 'update', :id => @product.id}) do |f| %>

which generates:

<form method="post" action="/aircons/8" accept-charset="UTF-8">

and I get the following error:

The action '8' could not be found for ProductsController

when trying to update a product with id=8.

I think that form's method should be put. Is that right ? How should I fix this ?


Some controller code:

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 contains only the following two lines:

root :to => "products#index"
resources :products

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖哒 2024-10-14 16:22:52

你为什么不直接使用:

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

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

Why don't you just use:

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

?

If it won't work, please add your routes to question.

少女七分熟 2024-10-14 16:22:52

尝试使用这个

<% 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 以获取有关此内容的更多信息看看这个

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

try using this

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

you need not do all the things that you are trying. If you have created this Product model using scaffolding mechanism you must have its entry in the config/routes.rb file this will give you a path variable as below

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

you can get the edit path as edit_product_path for more info on this have a look at this

Hope you understand it better now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文