简体   繁体   中英

Rails : instance variables does not remain when reload page

I have a HTML page, in that page, there some options for User to select, this will affect how content will display.

I save those options by using instance variable in controller. For example :

class ShopController < ApplicationController

def index
   @option1 = option1 # instance variable option1, contains some information on the form
end

end

In this page, I have a form for user input information. When user presses submit button, ShopControler will process those information. But at that time, when I check @option1 variable --> NIL. It means : this is an another instance of ShopController .

Please give me a solution for the problem that I meet, that I can save information, and can reuse although user has summit form.

Thanks :)

Have you thought about storing it in a session variable?

session[:option1] = option1

Then when you're done with it just do a

session.delete(:option1)

This will allow you to use it through multiple requests.

Typically when a user submits a form it will be routed to the create or update action. Unless you specifically have it routed to the index action your problem is most likely that you're not setting @option1 in the action the form is being submitted to.

If you find you are setting it in every action consider using a before_filter to set it.

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