Russ
2006-Jul-29 23:04 UTC
[Rails] Instance Variables within Controller during AJAX request
Hey all, I''m trying to do something sorta simple... There are three button ids: ''view'', ''build'', ''search'' My controller looks like this: class RandomController < ApplicationController before_filter :login_required, :except => [:index, :initialize] def initialize @mode = ''view'' end def index store_location end def update_mode render :update do |page| page[@mode].className = ''tab'' page.alert(@mode) @mode = params[:id] page[@mode].className = ''tab active'' end end end Now, I''m using link_to_remote for all of the buttons and the id of each button when clicked is coming through fine. Each tab is correctly set to "tab active" when clicked on. However...the problem is that it seems during an AJAX (remote) request it makes a NEW instance of the controller when it dispatches and thus the @mode = ''view'' every time in spite of setting it during the update_mode function. I tried putting a :before_filter in application.rb to set mode instead but that didn''t work either. Is there any way to have the AJAX request see an updated @mode variable when it executes the update_mode code (that rhymes!)? Thanks, Russ -- Posted via http://www.ruby-forum.com/.
Steven Talcott Smith
2006-Jul-30 03:27 UTC
[Rails] Re: Instance Variables within Controller during AJAX request
Russ wrote:> Hey all, > I''m trying to do something sorta simple... > There are three button ids: ''view'', ''build'', ''search'' > > My controller looks like this: > > class RandomController < ApplicationController > before_filter :login_required, :except => [:index, :initialize] > def initialize > @mode = ''view'' > end > def index > store_location > end > > def update_mode > render :update do |page| > page[@mode].className = ''tab'' > page.alert(@mode) > @mode = params[:id] > page[@mode].className = ''tab active'' > end > end > end > > Now, I''m using link_to_remote for all of the buttons and the id of each > button when clicked is coming through fine. Each tab is correctly set to > "tab active" when clicked on. However...the problem is that it seems > during an AJAX (remote) request it makes a NEW instance of the > controller when it dispatches and thus the @mode = ''view'' every time in > spite of setting it during the update_mode function. I tried putting a > :before_filter in application.rb to set mode instead but that didn''t > work either. Is there any way to have the AJAX request see an updated > @mode variable when it executes the update_mode code (that rhymes!)? > > Thanks, > RussRuss, the way to share data between requests is to the session hash. Instead of @mode, try session[:mode] @mode is only good for sharing data with the view during the processing of a single request. Steven -- Posted via http://www.ruby-forum.com/.
Russell
2006-Jul-30 21:43 UTC
[Rails] Re: Instance Variables within Controller during AJAX request
Steven,> Russ, the way to share data between requests is to the session hash. > Instead of @mode, try session[:mode] > > @mode is only good for sharing data with the view during the processing > of a single request. > > StevenYup, I ended up going this way shortly after my post. Thanks for the help, I appreciate it! =) Russ -- Posted via http://www.ruby-forum.com/.