开发者

How do I set up my @product=Product.find(params[:id]) to have a product_url?

开发者 https://www.devze.com 2023-01-03 22:15 出处:网络
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?

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])
 开发者_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.

0

精彩评论

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