PUT-ing a form to update a row, but I can't find the id. Where is it?
Posted
by montooner
on Stack Overflow
See other posts from Stack Overflow
or by montooner
Published on 2010-06-16T22:00:55Z
Indexed on
2010/06/16
22:02 UTC
Read the original article
Hit count: 180
How should I be passing in the ID?
Error:
Couldn't find Product without an ID
Form:
<% 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 %>
Controller (for /products/edit/1 view):
def edit
@product = Product.find(params[:id])
end
Controller (to change the db):
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
© Stack Overflow or respective owner