In my noddy blog app I want to not have to scroll back down after deleting an entry.
I first tried using Javascript scrollTop in the view, but I could not find a way of passing the value to the controller so that I could pass it back to the view. I hope an expert can describe what methods, if any, can be used to pass values from a view to an action (without using a form). This seems to be poorly documented.
So I decided to use anchors instead (which is probably the correct way). Each blog entry in the view gets an id=X attibute (X = 1,2,3,etc). If I delete entry 4 then I want to scroll to entry 4-1=3.
In the index view:-
<% for comment in @comments %> <% return_to=return_to+1 %> <div class="comment" id="<%= return_to %>"> ... <%= link_to 'Delete', comment_path(:id => comment.id, :return_to => return_to-1, :post_id => @post),:method => :delete %>
Hovering over the delete link of entry 4 I see the following URL displayed at the bottom of FireFox:-
http://localhost:3000/comments/47?return_to=3&post_id=12
In the destroy action I redirect_to index and specify an :anchor parameter:-
format.html { redirect_to :action => 'index', :anchor => params[:return_to],:post_id => params[:post_id] }
The :anchor parameter is automatically propagated thru the :index action as it appears in the browser's URL field. :-
http://localhost:3000/comments?post_id=12#3
Unfortunately the browser does not actually scroll down to the specified anchor.
However if I then hit Enter on the URL it does jump to the anchor!!
Does anyone know how to get anchors working properly?
PS as you can see I am using REST but probably not to the full extent, since I have post_id=23 when in fact I should probably have something like/post/23/comment/56. but that is another issue
Peter