Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Rails render a view and automatically scroll down?

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
like image 567
icecream Avatar asked Jan 29 '11 22:01

icecream


3 Answers

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" })
like image 104
Another Sam Avatar answered Oct 18 '22 05:10

Another Sam


http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html

redirect_to profile_path(@profile, :anchor => "wall")

That is how you pass anchors.

like image 5
thenengah Avatar answered Oct 18 '22 03:10

thenengah


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 %>
like image 3
joaopribs Avatar answered Oct 18 '22 05:10

joaopribs