views:

20

answers:

1

Hi,

In the update action of my preferences controller in my Rails app, if there are any errors in the validation/save etc there's a call to:

format.html { render :edit }

Nothing too unusual there - however, when this code is hit, the address in the browser changes and loses the /edit in the URL.

For example:

To start with, my browser shows I am on the page at following address: http://localhost:3000/preferences/1/edit

However, once the errors are detected and the render is called the address in the is changed to http://localhost:3000/preferences/1

I can't say I've ever noticed this behaviour before - but Is there a way to force the /edit to stay on the end of the URL? Without the /edit it is effectively showing the URL of the show page (and I don't have a template for that!)

Many thanks, Ash

+2  A: 

Instead of calling render, you can redirect_to the edit page, and use flash to keep track of the model:

def update
  # ...
  if [email protected] # there was an error!
    flash[:model] = @model
    redirect_to :action => :edit
  end
end

And then in the edit action you can reload the values from flash[:model], ie:

def edit
  if flash[:model]
    @model = flash[:model]
  else
    @model = ... # load model normally
  end
end
Daniel Vandersluis
Fantastic! Thanks! I'd been trying to use redirect_to but was losing the flash messages!
Ash