Hello, I want make a variable visible to all actions in a controller. Everytime user submits a parameter "school_class_id" on the basis of which I assign a variable which I want to make available to all the actions in controller. I used before_filter: set_class_id where set_class_id is defined as def set_class_id @currentClassid = session[:currentclassid] if !params[:class_list].nil? if !params[:class_list][:class_id].nil? @currentClassid = params[:class_list][:class_id] end end end In this case, value of @currentClassid changes in index action. However in other actions, it remains the same as the value it is assigned intially for the first time ( In this case , the value in the session ). I am clueless why this is happening. If in index action, the value of @currentClassid is changing, then why not in other actions? -- Thanks, Jaikishan "Enjoying the rights of being intelligent ignorant" --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Sep-22 07:19 UTC
Re: Variables visible in all actions in a controller
Jaikishan Jalan wrote:> Hello, > I want make a variable visible to all actions in a controller. Everytime > user submits a parameter "school_class_id" on the basis of which I assign a > variable which I want to make available to all the actions in controller. > > I used before_filter: set_class_id where set_class_id is defined as > > def set_class_id > @currentClassid = session[:currentclassid] > if !params[:class_list].nil? > if !params[:class_list][:class_id].nil? > @currentClassid = params[:class_list][:class_id] > end > end > end > > In this case, value of @currentClassid changes in index action. However in > other actions, it remains the same as the value it is assigned intially for > the first time ( In this case , the value in the session ). I am clueless > why this is happening. If in index action, the value of @currentClassid is > changing, then why not in other actions?How could it change? Instance variables (by definition) are restricted to an instance of a class. When you view another action the filter runs again and @currentClassid is set to session[:currentclassid]. If you do want it to persist across actions you could put in back in the session after setting it.> > -- > Thanks, > Jaikishan > > "Enjoying the rights of being intelligent ignorant"--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mon, Sep 22, 2008 at 9:05 AM, Jaikishan Jalan <jai.ism-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I want make a variable visible to all actions in a controller. Everytime > user submits a parameter "school_class_id" on the basis of which I assign a > variable which I want to make available to all the actions in controller. > I used before_filter: set_class_id where set_class_id is defined as > def set_class_id > @currentClassid = session[:currentclassid] > if !params[:class_list].nil? > if !params[:class_list][:class_id].nil? > @currentClassid = params[:class_list][:class_id] > end > end > end > In this case, value of @currentClassid changes in index action. However in > other actions, it remains the same as the value it is assigned intially for > the first time ( In this case , the value in the session ). I am clueless > why this is happening. If in index action, the value of @currentClassid is > changing, then why not in other actions?If that filter runs in all actions, and the params are received, the instance variable should store what''s in params. I guess in actions other than index the params are not being sent. A note on conventions: in Ruby instance variables use snake case: @current_class_id. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---