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

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.

Steven

Yup, I ended up going this way shortly after my post. Thanks for the
help, I appreciate it! =)

Russ

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,
Russ

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.

Steven

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs