views:

17

answers:

1

Trying to recreate { script/generate scaffold }, and I've gotten thru a number of Rails basics. I suspect that I need to configure default product url somewhere. But where do I do this?

Setup:

  1. Have: def edit { @product=Product.find(params[:id]) }
  2. Have edit.html.erb, with an edit form posting to action => :create
  3. Have def create { ... }, with the code redirect_to(@product, ...)
  4. Getting error: undefined method `product_url' for #< ProductsController:0x56102b0>

My def update:

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: 

Ah, add in /config/routes.rb the line:

map.resources :products

and make sure you put that above the default:

map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'

This defines a system for giving :product's urls.

montooner