views:

471

answers:

3

Hi, this seems simple but I can't figure it out. I receive post data in my rails app through a form and I want to use a redirect (instead of a render) but keep at least one of the pieces of post data alive through the redirect. How can I do this? Adding a hash on to redirect_to doesn't seem to work.

+1  A: 

When i have to do this in PHP, i store the POST data in the session, then i remove them when i'm done using them (There's even a pattern called "post / redirect / get" that tell to do this to avoid re-post of submitted data when reloading the page).

Couldn't you do what you want this way or something similar with Rails?

p4bl0
I can do this with the session. The only reason I'm hesitant is that I'm not quite sure where to clear the variable (I don't really want to be resetting it on every page load). If I don't reset the variable it will interfere down the road.
William
Couldn't you reset the variables just after you've finished using it in the page which you want to `redirect_to`? (Maybe what i'm saying is stupid, I don't really know RoR)
p4bl0
Don't worry that's not stupid, the only reason I can't do that is that I don't know what page the redirect is going to (it's set to redirect to the previous http request or the index page if one isn't set).
William
Then maybe you could add `?redir` at the end of the url which you redirect to and then you reset the session variables if there is a `redir` in the GET parameters of your url, but that means you have to test it on each page, so it may be as bad as the previous solutions...
p4bl0
A: 

May I ask you why do you need to do that?

You say that you don't know in advance where the page will be redirected to; also, you said that you aren't sure about using session because you wouldn't know when to reset it.

But, if every action (or every action in a set) can receive that post, I assume that it will not just ignore it, but that it will use it instead, so aren't you already somehow checking to see if you have to reset the session on each action?

giorgian
sorry, maybe this should have been a comment and not an answer...
giorgian
A: 

I think what you're looking for is flash.

Andy Gaskell