# Scope problem with form\_remote\_tag, ajax?

**URL:** <https://www.ruby-forum.com/t/scope-problem-with-form-remote-tag-ajax/66871>\
**Category:** Rails\
**Created:** [August 29, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/scope-problem-with-form-remote-tag-ajax/66871 "2006-08-29T23:00:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [August 29, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/scope-problem-with-form-remote-tag-ajax/66871/1 "2006-08-29T23:00:31Z")

</div>

Hi all,

I have tracked an odd behavior down to what I think is an issue of  
scope. In short, when I use form\_remote\_tag to establish an AJAX form,  
my database fields don’t get populated when performing an edit on the  
record. Those same fields will get populated using start\_form\_tag and  
no AJAX. I think that Ruby can’t find the local object that represents  
the database record…here’s why:

Up front info: table is Issues, one of the text fields in question is  
file\_number. My use of ajax is to put a subset of the table’s fields on  
different tabs in a notebook style. In this example, when the user  
clicks on the appropriate link, the fields I’ve considered descriptive  
are displayed, and file\_number is one of those.

A1. Entering issues\_controller.rb  
A2. call to issues\_controller::edit()

- B1. within edit, @issue is assigned to issue.find(params[:id])
- B2. implicit call to render edit.rhtml  
– C1. Rendering edit.rhtml  
– C2. form setup with call to form\_remote\_tag  
— D1. call to render(:partial =\> ‘form’)  
---- E1. Rendering \_form.rhtml  
---- E2. ajax call using link\_to\_remote( :action =\>  
:tab\_description\_fields )  
---- (target of ajax call is to a div within the \_form.rhtml file)  
------ F1. Entering issue\_controller::tab\_description\_fields()  
------ F2. call to render(:layout =\> false)  
------ F3. implicit transfer to render of tab\_description\_fields.rhtml  
------- G1. Entering tab\_description\_fields.rhtml  
------- G2. call of text\_field(‘issue’, ‘file\_number’)

Hopefully you can follow my whacky scope stack. I’ve been able to  
verify that the local var ‘@issue’ that is declared in B1 is available  
at E1 (i.e. if I stick the call to text\_field(‘issue’,‘file\_number’) in  
on either side of E2, it gets populated just fine). Once I get into the  
tab\_description\_fields() function and its call to  
tab\_description\_fields.rhtml, the ‘@issue’ var doesn’t seem to be in  
scope anymore, so none of the field helpers end up making the link back  
to the issue model.

Is there something obvious that I’m missing? Any help or suggestions is  
greatly appreciated.

Thanks,

- David

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [August 30, 2006, 4:12am UTC](https://www.ruby-forum.com/t/scope-problem-with-form-remote-tag-ajax/66871/2 "2006-08-30T04:12:48Z")

</div>

AHA! I figured it out. If anyone else is interested, here’s what I  
learned…

It is indeed a fact that my instance variable ‘@issue’ disappeared from  
scope once the template called another function via ajax. A controller  
function foo() can set an instance var like @myfoovalue, and the  
associated template, foo.rhtml, will have access to that var via “\<%=  
@myfoovalue %\>” but if that template calls other code, the var is not  
carried along.

So, the trick is to use the session object to carry this state around.  
Using my earlier example, here’s how I did it:

Within issue\_controller.rb:

def edit  
@issue = Issue.find(params[:id])  
# save the issue id into the session object  
session[:curr\_issue] = @issue  
# default behavior is to render edit.rhtml now  
end

edit.rhtml sets up the form, and it calls partial render on \_form.rhtml  
which looks like this:

\<%= link\_to\_remote(‘Description’,  
:update =\> ‘tabbed\_area’,  
:url =\> { :controller =\> ‘issues’,  
:action =\> :tab\_description\_fields } ) %\>

\<%= link\_to\_remote(‘Property Details’,  
:update =\> ‘tabbed\_area’,  
:url =\> { :controller =\> ‘issues’,  
:action =\> :tab\_property\_fields } ) %\>

\<%= link\_to\_remote(‘Financials’,  
:update =\> ‘tabbed\_area’,  
:url =\> { :controller =\> ‘issues’,  
:action =\> :tab\_financial\_fields } ) %\>

\<%= link\_to\_remote(‘Messages’,  
:update =\> ‘tabbed\_area’,  
:url =\> { :controller =\> ‘issues’,  
:action =\> :tab\_message\_fields } ) %\>

Now, back in issue\_controller.rb, the functions that correlate to tabs  
on the notebook look like:

# these functions call their associated \*.rhtml files without

rendering the standard templates,

# e.g. tab\_financial\_fields will trigger tag\_financial\_fields.rhtml

def tab\_financial\_fields  
@issue = session[:curr\_issue]  
render(:layout =\> false )  
end

def tab\_description\_fields  
@issue = session[:curr\_issue]  
render(:layout =\> false )  
end

def tab\_property\_fields  
@issue = session[:curr\_issue]  
render(:layout =\> false )  
end

def tab\_message\_fields  
@issue = session[:curr\_issue]  
render(:layout =\> false )  
end

Now, the templates that are associated with the tab\_\* functions get a  
valid @issue instance variable and all their text\_area calls work just  
dandy.

- David
