I need some clearance on scope of instance variables in rails application (not sure if other frameworks have the similar paradigm)
Lets say I have an instance variable
@page_count = some_value
inside a controller’s index action
So, regarding accessibility of this variable. It should be accessible within its view and I guess from model as well.(right?)
Now, if i call some other action(through other request) say, export in the same controller, the variable @page_count wont have its value which was set inside index action
(instance variable would not retain its value between different requests, right?)
Currently to deal with this I am using session scoped variable,
session[page_count] = some_value # inside index action
Now the variable will be available inside other actions, including the export action ( through other request)
Is this is good approach? using sessions? or there are better alternatives for storing values between different requests?
Suggestions/Comments/pointers, please?
I think it depends on what you set
@page_countto.If it’s like the number of rows in a db table ‘pages’, and you set it like,
Then you could use a
before_filterand set the value there for every controller action you need it. Here’s some more info on filters.Otherwise, if the value of @page_count is something calculated and changes per action, then I think you just have to define it in every action.