views:

57

answers:

2

I have this structure for the table course:

Search page -> Result Page -> Edit Page -> Show page

When i go from result page to a specific course edit page, i edit it, and end up on the show page, but when i hit back, i get a loop from show to edit, and back to show and so on.

I want the edit page to back to the result page if it came from there.

im using this on both:

<%= link_to "Back", :back %>
+2  A: 

When you actually update your record having edited it you're likely to be doing a redirect from an update action via a put request to show. Even if you're not, and if you're defying convention and updating from the show action, you're trying to navigate to a post action with a get request. If I understand you correctly, you want to be able to edit from either the search result or the show page. What you should do is define a method that allows you to store a location in the session on demand. Put it in the application controller and it will be available to all of your controllers.

# copy this into your application_controller.rb file :
private
def store_location
  session[:return_to] = request.request_uri
end

#copy this to the top of your item_controller.rb file:
before_filter :store_location, :only => [:search, :show]

#replace your <%= link_to "Back", :back %> with
<%= link_to 'back', session[:return_to] -%>
mark
You'd want `request.env['HTTP_REFERER'] ` instead of `request.request_uri`. And it is also possible to use `session[:return_to] || url_for(:back)` in the `link_to` so there's a fallback in case something odd happened.
shmichael
A: 

I tried the simpler part of your explanation, ending with the same result, probably did something wrong, since i got a little confused with the explanation xD. I'd like it to work like a stack, so when i come from search, i edit, end up in show page, then when i hit back, and back again, i end up on the search result. And im gonna remove the edit link on the show page to reduce problems.

onildo
There's only one explanation really and you'd need to do everything I explained. I'll add some comments to my answer. :)
mark
I had to adjsut some stuff, because just like that it didn't work.I used another session variable, wich is loaded every time i enver the search page, to check if the search was acessed before. Its not very pretty but it works, thank you for the help ^^
onildo