Rahul
Rahul

Reputation: 47106

how am i supposed to handle this?

i am saving a product in my create action, once it is saved i would like it to redirect it to show action like this.

if @product.save
  redirect_to :action => :show

and my show action displays the content of the product with the requested id. Since I am redirecting, I would lose all my instance variables. So how to pass the id? Should I use flash for this? Or is there a better way of doing this

Upvotes: 0

Views: 51

Answers (1)

Reuben Mallaby
Reuben Mallaby

Reputation: 5763

The scaffold generator produces code like:

if @product.save
  redirect_to @product
else
  render :new
end

Rails will look to use a product_path pointing to the show action with the :id being @product.id

This assumes you have, in your config/routes.rb

Rails 2.X

map.resources :products

Rails 3.X

resources :products

Upvotes: 3

Related Questions