Topic: Adding anchor to URL when rendering an action containing error message

I want to add an anchor to my view when a validation error occurs. Currently the page redisplays, but my error message is below the and of the screen and cannot be seen unless the user scrolls down. I want the browser to automatically scroll down to the message.

Here is my method in the controller:

def interaction
    @interaction = Contact.find(params[:id]).interactions.create(params[:interaction])
    @interaction.date_created = Date.today
    if @interaction.save
      flash[:notice] = 'The interaction was added'
      redirect_to :action => 'show', :id => params[:id]
    else
#this line renders the action with the errors, but I need to add an anchor to the URL to display
#the error
      @contact = Contact.find(params[:id])
      render :action => 'show', :id => params[:id]
    end 
end

and in the Interaction model I have a validate_presence_of call, which generates the error if that field is empty.

Any ideas?

Stu

Re: Adding anchor to URL when rendering an action containing error message

You can't add an anchor to a render call - only a redirect call.  When you render a view it simply uses the current url (the one that was used to get to the current controller) and spits out some html.

Why is it that you need the anchor while displaying errors?

Re: Adding anchor to URL when rendering an action containing error message

Thanks for the reply. I need to add an anchor as the error message is displayed quite far down the page, which means when the view is rendered, the error message is only seen by the user if the user scrolls down. If I add an anchor, then the browser will re-adjust to the error message. Any other suggestions for a solution?