I have an obnoxiously large landing page with a registration form in the middle of it. If I submit the form and validation fails, I want to render the landing page again, but I want it be scrolled down to the registration form so they can see the errors and make edits. Is this possible to jump down to the form with the render
method, or do I need to do redirect_to "account/new#theFormID"
?
I would rather not do a redirect because you have to save the form information in a session, repopulate the form, et cetera, and I want to stick the conventional
if @resource.save then redirect_to ...
else render "new"
end
Put the anchor directive on the form and you can still rely on :render from the controller...
form_for(@my_model, :url => { :action => "create" , :anchor => "my_anchor" })
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html
redirect_to profile_path(@profile, :anchor => "wall")
That is how you pass anchors.
I scrolled to an anchor with render
using a global var and Javascript (as hinted by Ibrahim).
On my controller I had this:
@anchor = "my_anchor"
render :action => "edit"
And on my page, I checked if I had a global @anchor, and if so, roll to it with Javascript:
<% if @anchor %>
<script type="text/javascript">
window.location.hash = "<%= @anchor %>";
</script>
<% end %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With