问题
I am using multiple redirects, and I would like to redirect from A->B->C->A.
So in B, I save path A as
@previouspage = request.referer
and so @previouspage = A at this point, but when I call @previouspage in C, it doesn't bring the hard value saved in B, but finds its own relative request.referer, which is B.
So in C, @previouspage = B (because I think variables in Ruby are soft-links)
How would I just save whatever the value of request.referer was at point B, and then save that URL into a variable that I can access later?
回答1:
HTTP is a stateless protocol: variables are not remembered between requests. If you want to save state between requests, then you might use a session. In Rails it is done like this:
In B:
session[:page_a] = request.referer
In C:
@next_page = session[:page_a]
The Rails Security Guide begins with sessions and their vulnerabilities. You might want to check it out.
回答2:
Rails variables are not kept between requests. If you want to save some data, you should:
- Save it in a (session) cookie
- Save it in the database
- Pull some tricks out of your sleeve and pass the referer as a parameter or something like that but it is not recommended at all.
回答3:
Three things needed to get this working
- Using sessions: sessions[:original_page]=request.referrer
- Accounting for the fact that sessions get refreshed after login (check if nil)
- Remembering to set session[:original_page]=nil right after using it in the controller.
来源:https://stackoverflow.com/questions/11393687/ruby-on-rails-how-to-assign-a-hard-value-to-a-variable