简体   繁体   中英

Rails - how do I move a variable from one controller action to another without resetting it

I have a variable which contains a certain date based on certain criteria, which is established in the index action of my controller:

def index
    if params[:y] && params[:m]
      @date = ...
    elsif
      @date = ...
    else        
      ...

It is a bit more complicated than the example above, but you should get the idea. If I then want to use this variable within another action of the same controller, how do I do that? I would like it to be something like:

def new
  @today = see index action
...

Thanks

There is No Way.

Every request creates an instance of controller. When hitting #index , there is an instance. And the instance variables you defined can live there. When hitting "new", that's a new instance, the old instance variables are no longer there.

To remember something between requests, you can

  1. Use query strings.
  2. Use session
  3. Save to db(as user preference).
  4. Use cookie(not nice)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM