# Passing objects to methods

**URL:** <https://www.ruby-forum.com/t/passing-objects-to-methods/161413>\
**Category:** Rails\
**Created:** [February 26, 2009, 10:15am UTC](https://www.ruby-forum.com/t/passing-objects-to-methods/161413 "2009-02-26T10:15:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [February 26, 2009, 10:15am UTC](https://www.ruby-forum.com/t/passing-objects-to-methods/161413/1 "2009-02-26T10:15:26Z")

</div>

hey guys,

I need to pass an object to a ruby method, which handles an Ajax call.  
I would appreciate if you could tell me a convenient way to do so.

Here is some of my javascript for the Ajax:

function submitAjax(evt){  
evt.preventDefault();  
url = “\<%= url\_for(:controller =\> ‘groups’, :action =\> ‘search’,  
:canvas =\> false, :only\_path =\> false ) %\>”;  
form = document.getElementById(‘search\_group\_form’);  
submitForm( form , url,‘search\_team\_results’);  
return false;  
}

I am not sure if there is a way to pass an object in the url string  
above. I tried the following:

url = "\<%= url\_for( …, :action =\> ‘search’, :myobject =\>  
#{@myobject} …

but it didn’t work.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [February 26, 2009, 10:18am UTC](https://www.ruby-forum.com/t/passing-objects-to-methods/161413/2 "2009-02-26T10:18:58Z")

</div>

maybe you can save object to session and send a flag to get the object?

On Thu, Feb 26, 2009 at 5:15 PM, Pesho P. \<  
removed\_email\_address@domain.invalid\> wrote:

> url = "\<%= url\_for(:controller =\> ‘groups’, :action =\> ‘search’,  
> #{@myobject} …
> 
> but it didn’t work.
> 
> ## Thanks in advance!
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
TWRUG Blog:  
[http://blog.rubyonrails.org.tw](http://blog.rubyonrails.org.tw)

CFC on Rails:

> **[CFC on Rails](http://zusocfc.blogspot.com)**
>
> 火車請別撞我

Only two surfaces of a box:  
[http://blog.pixnet.net/zusocfc](http://blog.pixnet.net/zusocfc)

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [February 26, 2009, 11:37am UTC](https://www.ruby-forum.com/t/passing-objects-to-methods/161413/3 "2009-02-26T11:37:52Z")

</div>

Billy H. wrote:

> maybe you can save object to session and send a flag to get the object?

Thanks a lot for the hint. The idea seems very nice. However, I can’t  
get it working. I’m using the Facebooker plugin, maybe it messes up with  
sessions.

in my index method of the main controller, I create the session  
variable:  
session[:me] = @me

then in another method I try to use print it’s contents:  
print “\n” + session[:me].account\_id.to\_s

However, I get an error: “You have a nil object when you didn’t expect  
it!”

So the variable is not preserved in the session. Am I doing something  
wrong with storing/retrieving the session objects?

Thanks again!

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [February 26, 2009, 11:37am UTC](https://www.ruby-forum.com/t/passing-objects-to-methods/161413/4 "2009-02-26T11:37:53Z")

</div>

storing whole objects in session isn’t usually a good recommendation…  
the best solution would probably be to pass just the object’s id and  
then retrieve it from the database/wherever in your requested action  
another approach could be to serialize the object and deserialize it  
at the server. however, if you just need to pass a hash or something  
like that, you could use the json notation to pass the data

Regards

On Thu, Feb 26, 2009 at 10:17 AM, CFC [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Here is some of my javascript for the Ajax:  
> > I am not sure if there is a way to pass an object in the url string  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).  
> > [http://zusocfc.blogspot.com](http://zusocfc.blogspot.com)
> 
> Only two surfaces of a box:  
> [http://blog.pixnet.net/zusocfc](http://blog.pixnet.net/zusocfc)
> 
> >

–  
/\*\*

- dagi3d v4 | [http://dagi3d.net](http://dagi3d.net)  
\*/

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [February 26, 2009, 11:59am UTC](https://www.ruby-forum.com/t/passing-objects-to-methods/161413/5 "2009-02-26T11:59:36Z")

</div>

what I wanted to say is that you should pass the object’s id through  
the ajax request so there is no need to store it in the session

regards

On Thu, Feb 26, 2009 at 11:55 AM, Pesho P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > >
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
/\*\*

- dagi3d v4 | [http://dagi3d.net](http://dagi3d.net)  
\*/

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [February 26, 2009, 11:55am UTC](https://www.ruby-forum.com/t/passing-objects-to-methods/161413/6 "2009-02-26T11:55:45Z")

</div>

hmmm, ok,

but even if I try this  
session[:me] = @me.account\_id

and try to print it in the other method  
print “\n” + session[:me].to\_s + “aaa”

it only prints the “aaa”

If I try to print it in the same method, after I have added it to the  
session, then it outputs the account\_id… grr  
How is it possible that the session is not preserved when I call its  
variables in another method.  
By the way, even when I stored a whole object in the session, I could  
print its contents in the same method where I stored it.

any ideas ☹

Borja MartÃ­n wrote:

> storing whole objects in session isn’t usually a good recommendation…  
> the best solution would probably be to pass just the object’s id and  
> then retrieve it from the database/wherever in your requested action  
> another approach could be to serialize the object and deserialize it  
> at the server. however, if you just need to pass a hash or something  
> like that, you could use the json notation to pass the data
> 
> Regards
> 
> On Thu, Feb 26, 2009 at 10:17 AM, CFC [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > Here is some of my javascript for the Ajax:  
> > > I am not sure if there is a way to pass an object in the url string  
> > > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).  
> > > [http://zusocfc.blogspot.com](http://zusocfc.blogspot.com)
> > 
> > Only two surfaces of a box:  
> > [http://blog.pixnet.net/zusocfc](http://blog.pixnet.net/zusocfc)
> > 
> > >
> 
> –  
> /\*\*
> 
> - dagi3d v4 | [http://dagi3d.net](http://dagi3d.net)  
> \*/

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [February 26, 2009, 12:19pm UTC](https://www.ruby-forum.com/t/passing-objects-to-methods/161413/7 "2009-02-26T12:19:16Z")

</div>

aah ok, that was one of the things I tried first.

however, I’m still not sure how I should do that syntactically.

As I mentioned in my first post, I tried to do something like:  
(in the controller): @myid = “15”  
Then in the javascript function, which issues the Ajax call I construct  
the following url for the Ajax call:

url = "\<%= url\_for(:action =\> ‘search’, :myid =\> #{@myid}, canvas =\>  
false…

Then I get a syntax error. I am not sure how I have to pass the variable  
in the Ajax url?!

And still, I am wondering why after putting a simple string into the  
session, I cannot retrieve it from another method (different than the  
one where I added the string)

Thank you!

Borja MartÃ­n wrote:

> what I wanted to say is that you should pass the object’s id through  
> the ajax request so there is no need to store it in the session
> 
> regards
> 
> On Thu, Feb 26, 2009 at 11:55 AM, Pesho P.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > > >
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > >
> 
> –  
> /\*\*
> 
> - dagi3d v4 | [http://dagi3d.net](http://dagi3d.net)  
> \*/

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [February 26, 2009, 1:48pm UTC](https://www.ruby-forum.com/t/passing-objects-to-methods/161413/8 "2009-02-26T13:48:45Z")

</div>

oh i just realized that in the .erb view files I can directly use rails  
variables. Sorry for the last question 😛

Thanks for the help!

Pesho P. wrote:

> aah ok, that was one of the things I tried first.
> 
> however, I’m still not sure how I should do that syntactically.
> 
> As I mentioned in my first post, I tried to do something like:  
> (in the controller): @myid = “15”  
> Then in the javascript function, which issues the Ajax call I construct  
> the following url for the Ajax call:
> 
> url = "\<%= url\_for(:action =\> ‘search’, :myid =\> #{@myid}, canvas =\>  
> false…
> 
> Then I get a syntax error. I am not sure how I have to pass the variable  
> in the Ajax url?!
> 
> And still, I am wondering why after putting a simple string into the  
> session, I cannot retrieve it from another method (different than the  
> one where I added the string)
> 
> Thank you!
> 
> Borja MartÃ­n wrote:
> 
> > what I wanted to say is that you should pass the object’s id through  
> > the ajax request so there is no need to store it in the session
> > 
> > regards
