Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect to a specific view from method controller

I have a controller called "products_controllers.rb" that have this method:

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

Whenever the product is save I want to redirect it to an specific view called "suppliers", that belongs to the product views, how can I do that? Thanks in advance!

like image 265
Mayo Avatar asked Apr 26 '26 01:04

Mayo


1 Answers

If you're using restful routes and there is a relationship where product has many suppliers, you could use:

format.html { redirect_to product_suppliers_url(@product) }

with something like this in your routes.rb:

map.resource :products do |product|
  product.resource :suppliers
end

or you could also just use this:

format.html { redirect_to :action => 'suppliers', :id => @product.id }
like image 136
nateleavitt Avatar answered Apr 28 '26 14:04

nateleavitt