views:

21

answers:

2

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
+2  A: 

Try removing the :product from the form_for statement

Edit: Try removing the url option also. The issue is that the URL which you are posting to does not have an id attribute.

Maz
didn't work, unfortunately
montooner
That leads to the "No action responded to 1. Actions: create, edit, index, new, show, and update" problem"
montooner
Try then adding a :id=> @product.id to your URL hash.Also, are you using map.resources in your routes?
Maz
the { :id => ... } works. But do I really have to put that in there? I feel like if I passed in the @product w/ all the identifying data, Rails must supply the id for me. And yeah, map.resources is done in routes.
montooner
What version of Rails are you using?
Maz
Rails version 2.3.8
montooner
+2  A: 

This form tag should be sufficent:

<% form_for @product do |f| %>

Make sure you have this line in your config/routes.rb:

map.resources :products

For extra credit, you can simplify loading the @product in your controller with a before_filter:

class ProductsController < ApplicationController
  before_filter :load_product, :only => [:show, :edit, :update, :destroy]
  def load_product
    @product = Product.find(params[:id])
  end
end
elektronaut
Thanks for the extra credit section! That's awesome. Always looking for that.
montooner