开发者

Ruby on Rails: Why "edit" action does not work?

开发者 https://www.devze.com 2023-01-31 01:25 出处:网络
In views/products/edit.html.erb I use: <%= form_for(:product, :url => {:action => \'update\', :id => @product.id}) do |f| %>

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 successfu开发者_Python百科lly."
    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


Why don't you just use:

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

?

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号