# Best way to pass ID variable to partials - global var?

**URL:** <https://www.ruby-forum.com/t/best-way-to-pass-id-variable-to-partials-global-var/135890>\
**Category:** Rails\
**Created:** [April 25, 2008, 10:11pm UTC](https://www.ruby-forum.com/t/best-way-to-pass-id-variable-to-partials-global-var/135890 "2008-04-25T22:11:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [April 25, 2008, 10:11pm UTC](https://www.ruby-forum.com/t/best-way-to-pass-id-variable-to-partials-global-var/135890/1 "2008-04-25T22:11:04Z")

</div>

I have several orders that are being updated in an internal data entry  
app. I start by going from an order to pop-up form that allows a user  
to select items to add to a field in the order. The pop-up form  
contains 2 partials for drag and drop functionality from AWD. The user  
starts by selecting a category in a drop down list. Then on select, the  
first partial is filled with the list of options. The user can then  
drag and drop from the first partial to the second.

Now, my question is how to keep the original order id available to the  
partials at all times.

Right now I have a global variable set in the action when the page  
loads, but I’m guessing that this might not be the best way to do this.  
The first partial needs the order id so that it can look up which items  
have already been selected so it doesn’t include those in the option to  
select. The second partial displays all of the items that have been  
selected.

Hope this makes sense.

Thanks.

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [April 26, 2008, 12:55am UTC](https://www.ruby-forum.com/t/best-way-to-pass-id-variable-to-partials-global-var/135890/2 "2008-04-26T00:55:33Z")

</div>

Becca G. wrote:

> Right now I have a global variable set in the action when the page  
> loads, but I’m guessing that this might not be the best way to do this.  
> The first partial needs the order id so that it can look up which items  
> have already been selected so it doesn’t include those in the option to  
> select. The second partial displays all of the items that have been  
> selected.

Use render :partial =\> ‘foo’, :locals =\> { :@bar =\> bar }.

You can call that from Ajax handlers, and ViewTests, too.

The :0 is a trick that allows \_foo.rhtml to check if the variable exists  
at all:

if @bar …

Without that, no element of :locals would be optional, because an if bar  
on a  
non-existent bar is a syntax error, and checking defined? all the time  
is odious!

–  
Phlip  
[O'Reilly Media - Technology and Business Training](http://www.oreillynet.com/onlamp/blog/2007/07/assert_latest_and_greatest.html)

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [April 28, 2008, 6:41pm UTC](https://www.ruby-forum.com/t/best-way-to-pass-id-variable-to-partials-global-var/135890/3 "2008-04-28T18:41:17Z")

</div>

Phlip wrote:

> Use render :partial =\> ‘foo’, :locals =\> { :@bar =\> bar }.
> 
> You can call that from Ajax handlers, and ViewTests, too.
> 
> The :0 is a trick that allows \_foo.rhtml to check if the variable exists  
> at all:
> 
> if @bar …
> 
> Without that, no element of :locals would be optional, because an if bar  
> on a  
> non-existent bar is a syntax error, and checking defined? all the time  
> is odious!

I’m not able to make that work.

The id of the order is selected in action 1 which then calls a partial.  
Once the partial is rendered, that partial calls action 2 - this creates  
the drag and drop. Action 2 needs to be aware of the order id to make  
it available to action 3 when the partials are refreshed with the new  
lists for drag and drop. I think that I’m missing something to make  
this happen or I have the locals set incorrectly.

I also need to keep in mind that multiple orders will be updated  
throughout the process so it’s not a situation where I can set it and  
forget it.

Are locals still the best way to pass the id to the other actions or do  
I need to set up a method in the model that somehow retains the order  
id?

Thanks.

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [April 28, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/best-way-to-pass-id-variable-to-partials-global-var/135890/4 "2008-04-28T20:36:40Z")

</div>

> Are locals still the best way to pass the id to the other actions or do  
> I need to set up a method in the model that somehow retains the order  
> id?  
> Fix locals or pass things around as instance variables  
> @id = x

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [April 29, 2008, 1:47am UTC](https://www.ruby-forum.com/t/best-way-to-pass-id-variable-to-partials-global-var/135890/5 "2008-04-29T01:47:58Z")

</div>

Roger P. wrote:

> Fix locals or pass things around as instance variables  
> @id = x

I’m still new at this so I apologize for asking such an elementary  
question.

This is what I have and maybe someone can help me figure out where I’m  
going wrong with passing variables.

For this example, let’s say I’m adding fruit to an order. The user will  
start on an order page, click on a link that opens a pop-up for fruit.  
The pop up gets the order ID from the page the opened the pop up. The  
pop up contains a select box and 2 partials with drag and drop  
functionality. Let’s say the select box let’s you select by color of  
fruit. I left off some of the code to shorten the post, but I explained  
what I’m looking for in the actions.

#controller

def get\_order  
@order = Order.find(params[:id])  
end

def get\_fruit\_list  
@fruit\_list = Items.find\_all\_by\_color(params[:color])

render :update do |page|  
page.replace\_html ‘fruit\_list’, :partial =\> ‘fruit\_list’, :locals =\>  
{:order\_id =\> @order.id }  
end  
end

get\_fruit\_selected  
#order number needed to add the fruit to the order  
@order = Order.find(params[:order\_id])

#code here --\> fruit is saved to the order list  
#code here --\> both partials are rendered to reflect the added fruit  
end

So what I’m missing here is how to pass that ID number from get\_order to  
get\_fruit selected.

THANKS!

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [April 29, 2008, 5:06am UTC](https://www.ruby-forum.com/t/best-way-to-pass-id-variable-to-partials-global-var/135890/6 "2008-04-29T05:06:24Z")

</div>

store it in the session or pass it as a parameter to your popup, I’d  
guess.  
-R

On Mon, Apr 28, 2008 at 5:47 PM, Becca G.
