rerender a page if validation fails

Hi    I have in routes.rb like map.resources :users

        Now I am trying to edit a user. What the url shows now is http://localhost:3000/users/1/edit       But my problem is , when I click on update button and if validations fail I am re rendering the edit page. And it happens properly. But the url becomes


           I can't figure it out . Please give a solution. Why this happens? So if a user goes to url and press enter it will show

Unknown action

No action responded to show. Actions: create, edit, new, and update

    My controller code for update is

  def update      @user = User.find(params[:id])       respond_to do |format|   if @user.update_attributes(params[:user])           flash[:notice] = 'User was successfully updated.'           format.html { redirect_to home_url }           format.xml { head :ok }         else [:error] = @user.errors.full_messages.join("<br />")           format.html { render :action => "edit" }           format.xml { render :xml => @user.errors, :status => :unprocessable_entity }         end       end   end

Thanks in advance Tom

Your error case rendered the edit template... pretty standard.

The path was called on a POST (the update). There is no corresponding GET version of that path. So when you try to go there in the browser you get an error.

Hi    I get error when I enter http://localhost:3000/users/1 from url

But if it is http://localhost:3000/users/1/edit No problem.     So my question is why in the else case it shows in the url

http://localhost:3000/users/1   instead of http://localhost:3000/users/1/edit


When you are click on submit to update the data, /users/1 url is called ..

Now if your data is valid and saved properly then it will redirect to another page...

If your data is not valid or not saved properly then it will render to edit page..

When you render to any page , it will only call view page and url remains same,

benefit of render the page is that object contain the current data as well as the errors (if any which are use to show on page).

Thanks Brijesh Shah

Hi Tom,

It is happening so because, when u clicked update, the browser had sent post request to the new url which is http://localhost:3000/users/1 which is nothing but update action in ur controller. In ur update action you are rendering the edit action if the validation failed. Which means that url on browser doesn't change but the page being viewed is still edit page. So when user clicks on the url and press enter, it sends get request (not post request) to http://localhost:3000/users/1, which actually invokes show action in your controller.

The browser behavior is actually normal and correct. If the user clicks on the url and press enter, it should be assume that user wants to see the data not edit.

So you actually need to define show action or redirect the user back to edit page when validation fails (but redirecting will not carry your validation error messages, so you may have to put some additional efforts to carry those validation error messages).