我有一个名为"products_controllers "的控制器。
def create
...
...
respond_to do |format|
if @product.save
???????
else
format.html { render :action => "new" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
每当产品保存时,我想将其重定向到一个名为"供应商"的特定视图,该视图属于产品视图,我该如何做到这一点?提前感谢!
如果你使用rest式路由,并且产品有多个供应商,你可以使用:
format.html { redirect_to product_suppliers_url(@product) }
在你的routes.rb:
map.resource :products do |product|
product.resource :suppliers
end
或者你也可以这样写:
format.html { redirect_to :action => 'suppliers', :id => @product.id }
类似redirect_to 'product/suppliers'
来源:http://guides.rubyonrails.org/layouts_and_rendering.html using-redirect_to
注释:下次一定要在你的问题文本中指定"in rails",因为视图和控制器被许多框架使用。
在你的控制器中:
def create
...
...
respond_to do |format|
if @product.save
???????
else
format.html { render :action => "suppliers" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
def suppliers
#Your suppliers code goes here
end
在routes.rb resources :venues do
member do
get 'suppliers'
end
end