Changing redirects in a Rails scaffold
May 4th, 2007 by Leons PetrazickisAfter 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
@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.
Posted in ruby |



