How do I set up my @product=Product.find(params[:id]) to have a product_url?
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:
- Have: def edit { @product=Product.find(params[:id]) }
- Have edit.html.erb, with an edit form posting to action => :create
- Have def create { ... }, with the code redirect_to(@product, ...)
- 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])
开发者_StackOverflow中文版 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
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.
精彩评论