Download DB2

Changing redirects in a Rails scaffold

May 4th, 2007 by Leons Petrazickis

After a successful form entry, a Rails scaffold redirects you to the list view of all items. This is controlled by app/controllers/customers_controller.rb, where “customers” is the name of the scaffold. You can easily change it to redirect to the edit view or the show view.

  def create
    @customer = Customer.new(params[:customer])
    if @customer.save
      flash[:notice] = ‘Customer was successfully created.’
      redirect_to :action => ‘list’
    else
      render :action => ‘new’
    end
  end
 

You want to change the redirect action from:

redirect_to :action => ‘list’
 

to

redirect_to :action => ’show’, :id => @customer
 

where ’show’ is the view that you want and customer is your model.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg
  • Reddit

Posted in ruby |

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.