views:

31

answers:

1

I'm working on a project using ruby on rails. I want to refresh the same page when the action is called or redirect to the page where the action is called.

How do i do this ?

+2  A: 

you can use 'render' to refresh a page, and 'redirect_to' to redirect to a page going through its controller.

if you want to store a page to redirect back to, you can use:

def store_location
 session[:return_to] = request.fullpath
end

def redirect_back_or(default)
  redirect_to(session[:return_to] || default)
  clear_return_to
end

def clear_return_to
  session[:return_to] = nil
end

This is taken from Michael Hartl's book, he uses similar code to redirect to the requested page after the user signs in.

http://railstutorial.org/chapters/updating-showing-and-deleting-users#sec:friendly_forwarding

Nada
What if i don't have any login and session associated with my page?
Jayaram
sessions are not related to login and are there unless you explicitly turn them off. they are a way to store data for a particular person browsing your site. I understand from your question that the page where the action is called is not the same all the time and you want to keep track of it. If so, you'll need to use sessions so that the values you store for one person doesn't interfere with another.If you only have one page that calls that specific action then you already know the path of that page, and you don't need to store it. so you can use 'redirect_to' or 'render' directly.
Nada