views:

479

answers:

4

I'm looking to use render like so:

render :action => 'page#form'

I also tried this:

render :template => 'site/page#form'

That didn't work either. The form on this particular page is at the very bottom, and if any errors occur on submission I'd hate for the user to be defaulted to the top of the page. I also need to use render (not redirect) because I need to retain the objects and their errors.

How can I render to target a specific anchor tag?

A: 

Use redirect_to so the the browser gets the heads-up to scroll to the anchor:

redirect_to :action => 'page', :anchor => 'form'

Eric Hill
Read again... I can't use redirect. I need variables to persist.
bobthabuilda
There's a previous question that includes an implementation of clone to persist the object(s), including errors, across a redirect: http://stackoverflow.com/questions/1536428/rails-validation-over-redirect
Eric Hill
+1  A: 

Use JavaScript to move the view to the right place. There's no way I know of to do otherwise.

<script>
  window.location = window.location.href + "#form";
</script>

Untested, but I think this should work.

François Beausoleil
+1  A: 

Believe I found a solution. For anyone else having this issue, pointing the form like so:

<%= form_tag '/page#form' do %>

Seems to have solved the problem.

bobthabuilda
A: 

I'd suggest using jQuerys validate plugin. You could even implement your own form helpers that use jquery validate plugin depending on what object attributes you need to validate.

Of course it's more work, but I believe it would pay off in the future.

Other than that - use ajaxified submission and process error messages after response is received.

Eimantas