代码之家  ›  专栏  ›  技术社区  ›  montooner

如何设置@product=product.find(params[:id])以获得产品的URL?

  •  1
  • montooner  · 技术社区  · 14 年前

    尝试重新创建脚本/生成scaffold,我已经学习了很多Rails基础知识。我怀疑我需要在某个地方配置默认的产品URL。但我该在哪里做呢?

    设置:

    1. 有: def edit@product=product.find(params[:id])
    2. Endo.Eng.Erb ,并将编辑表单发布到 操作=>:创建
    3. 定义创建…} ,代码重定向到(@product,…)
    4. 获取错误: 未定义<productsController的“product_url”方法:0x56102b0>

    我的DEF更新:

    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
    
    1 回复  |  直到 14 年前
        1
  •  2
  •   montooner    14 年前

    啊,添加/config/routes.rb行:

    map.resources :products
    

    并确保将其置于默认值之上:

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

    这定义了一个给予的系统:产品的URL。