开发者

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开发者_Go百科 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 %>


Use javascript?

<script>
window.location.hash="ANCHOR";
</script>


And another thing... if it's a link like I'm actually using, you have to add a unique param so the browser thinks it's submitting something new, otherwise it just refreshes without hitting the server. I'm using:

link_to "REFRESH", refresh_my_model_path(@my_model, :anchor => "my_anchor", :options => {:id => Time.now} )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜