使用respond_to维护上一页的参数

时间:2011-02-11 21:53:46

标签: ruby-on-rails validation redirect

我有一个模型产品属于模型商店(has_many产品)。

当我为商店创建新产品时,我正在使用此网址:

/products/new?store_id=4

当产品创建验证失败时,我将被重定向到:

/products

以下是此“创建”操作的respond_to:

respond_to do |format|
  if @product.save        
    format.html { redirect_to(@product.store, :notice => 'Product was successfully created.') }
    format.xml  { render :xml => @product, :status => :created, :location => @product }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @product.errors, :status => :unprocessable_entity }
  end
end

理想情况下,我想将失败的创建用户重定向到以前的确切网址,即:

/products/new?store_id=4

谢谢,

哈里斯

1 个答案:

答案 0 :(得分:0)

也许这有帮助?

@store_id = params[:store_id]

respond_to do |format|
  if @product.save        
    format.html { redirect_to(@product.store, :notice => 'Product was successfully created.') }
    format.xml  { render :xml => @product, :status => :created, :location => @product }
  else
    format.html { render :action => "new", :store_id => @store_id }
    format.xml  { render :xml => @product.errors, :status => :unprocessable_entity }
  end
end

This is where I got my inspiration.