Ruby on rails 使用respond_to维护上一页的参数
我有一个模型产品,它属于一个模型商店,那里有很多产品 当我为商店创建新产品时,我使用以下url:Ruby on rails 使用respond_to维护上一页的参数,ruby-on-rails,validation,redirect,Ruby On Rails,Validation,Redirect,我有一个模型产品,它属于一个模型商店,那里有很多产品 当我为商店创建新产品时,我使用以下url: /products/new?store_id=4 当产品创建验证失败时,我将被重定向到: /products 以下是对此创建操作的响应: respond_to do |format| if @product.save format.html { redirect_to(@product.store, :notice => 'Product was success
/products/new?store_id=4
当产品创建验证失败时,我将被重定向到:
/products
以下是对此创建操作的响应:
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
理想情况下,我希望在创建失败时将用户重定向回与以前相同的url,即:
/products/new?store_id=4
谢谢
哈里斯也许这有帮助
@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
答案就在这篇文章中,这只是一个设计建议,可能适用于你,也可能不适用于你,但值得一看:让商店属于产品。然后,您可以使用RESTful路线,例如/stores/1/products/5、/stores/2/products/5,因为在您的应用程序的未来,产品可能属于许多商店?感谢Zabba的建议,但我正在处理特定音乐艺术家的商店。所以我确信Bob Dylan store的产品永远不会成为Bruce Springsteen store的产品。谢谢Rafal-那篇文章起到了帮助作用,但我最终使用了一个带有flash的重定向来存储错误。