# How to retrieve an object from form\_remote\_tag?

**URL:** <https://www.ruby-forum.com/t/how-to-retrieve-an-object-from-form-remote-tag/120022>\
**Category:** Rails\
**Created:** [November 13, 2007, 3:14am UTC](https://www.ruby-forum.com/t/how-to-retrieve-an-object-from-form-remote-tag/120022 "2007-11-13T03:14:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![GlennNZ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7c8e57/32.png) [@GlennNZ](https://www.ruby-forum.com/u/GlennNZ)\
**Post date:** [November 13, 2007, 3:14am UTC](https://www.ruby-forum.com/t/how-to-retrieve-an-object-from-form-remote-tag/120022/1 "2007-11-13T03:14:36Z")

</div>

Hi everyone, my misadventures with rails continues…

If I have a cart object which contains CartItem objects and then use  
partials to display them, how do I go about getting a copy of a  
CartItem object to a controller method?

So if a partial displaying a CartItem had this code…

\<% form\_remote\_tag :url =\> { :action =\> :delete\_from\_cart, :item =\>  
cart\_item } do %\>  
\<%= submit\_tag “Delete” %\>  
\<% end %\>

…then how do I access the CartItem object from the delete\_from\_cart  
method.

At the moment I’m just trying to print out the name of the CartItem  
using something like…

def delete\_from\_cart  
print params[:item].name  
#or maybe  
my\_item = CartItem.new(params[:item])  
print my\_item.name  
end

…but neither work so obviously I’ve completely misinterpreted rails  
again. I get an error like.

NoMethodError (undefined method `name’ for “#\<CartItem:  
0x47dd9f4\>”:String):[/quote]  
Note that I can go \<%= cart\_item.name %\> from the partial and it  
prints to the browser fine, so there is a name method.

Any help will be great.

On a side note, where is the best place to go for Rails help? Is there  
one particular forum that is more active than the others?  
[forums.devshed.com](http://forums.devshed.com) is pretty quite and railsforum seems quite young.

---

<div class="post-metadata">

**Author:** ![GlennNZ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7c8e57/32.png) [@GlennNZ](https://www.ruby-forum.com/u/GlennNZ)\
**Post date:** [November 13, 2007, 5:50am UTC](https://www.ruby-forum.com/t/how-to-retrieve-an-object-from-form-remote-tag/120022/2 "2007-11-13T05:50:40Z")

</div>

You are effectively passing the id (and you may need to use :item =\>  
cart\_item.id unless you use :id like below) of the cart item in so you  
need to find it:  
\<% form\_remote\_tag :url =\> { :action =\> :delete\_from\_cart, :item =\>  
cart\_item.id } do %\>  
\<%= submit\_tag “Delete” %\>  
\<% end %\>  
my\_item = CartItem.find(params[:item])

btw, the idiomatic rails way is to pass it in as the id parameter like:

\<% form\_remote\_tag :url =\> { :action =\> :delete\_from\_cart, :id =\>  
cart\_item } do %\>  
\<%= submit\_tag “Delete” %\>  
\<% end %\>

my\_item = CartItem.find(params[:id])

-Bill

GlennNZ wrote:

> \<%= submit\_tag “Delete” %\>  
> #or maybe  
> prints to the browser fine, so there is a name method.
> 
> Any help will be great.
> 
> On a side note, where is the best place to go for Rails help? Is there  
> one particular forum that is more active than the others?  
> [forums.devshed.com](http://forums.devshed.com) is pretty quite and railsforum seems quite young.
> 
> >

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![GlennNZ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7c8e57/32.png) [@GlennNZ](https://www.ruby-forum.com/u/GlennNZ)\
**Post date:** [November 13, 2007, 7:46am UTC](https://www.ruby-forum.com/t/how-to-retrieve-an-object-from-form-remote-tag/120022/3 "2007-11-13T07:46:23Z")

</div>

Great, thanks for clearing that up. There is one thing though, my  
understanding is that the find method is inherited from ActiveRecord  
so that going

\<% form\_remote\_tag :url =\> { :action =\> :delete\_from\_cart, :id =\>  
cart\_item } do %\>  
\<%= submit\_tag “Delete” %\>  
\<%end%\>

is dependent on CartItem being an ActiveRecord object. As it turns  
out, CartItem contains a Service object which inherits from  
ActiveRecord so I’ve gone

… :id =\> cart\_item.service.id …  
and  
my\_service = Service.find(params[:id])  
accordingly.

My question is this. Is it possible to create my own find method for  
the CartItem class so that it can look for an instance of CartItem?  
For example, using  
… :id =\> cart\_item…  
and then  
print params[:id]  
prints out something like #CartItem:0x471a5a8

so is it possible to create my own find method for CartItem using that  
piece of information i.e.  
def self.find(id)

# something really clever

end

Cheers

---

<div class="post-metadata">

**Author:** ![GlennNZ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7c8e57/32.png) [@GlennNZ](https://www.ruby-forum.com/u/GlennNZ)\
**Post date:** [November 13, 2007, 11:41am UTC](https://www.ruby-forum.com/t/how-to-retrieve-an-object-from-form-remote-tag/120022/4 "2007-11-13T11:41:39Z")

</div>

On Nov 13, 11:33 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > … :id =\> cart\_item…  
> > Well if you want instances of cart\_item to persist across requests you  
> > Cheers  
> > I have a Cart object which has an Array of CartItems which in turn  
> > holds a Service object which inherits from ActiveRecord. The Cart  
> > object is stored in the session variable so yes, it is persistent and  
> > therefore all the instances of CartItem should be persistent too.

---

<div class="post-metadata">

**Author:** ![GlennNZ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7c8e57/32.png) [@GlennNZ](https://www.ruby-forum.com/u/GlennNZ)\
**Post date:** [November 13, 2007, 11:34am UTC](https://www.ruby-forum.com/t/how-to-retrieve-an-object-from-form-remote-tag/120022/5 "2007-11-13T11:34:01Z")

</div>

On 13 Nov 2007, at 06:45, GlennNZ wrote:

> My question is this. Is it possible to create my own find method for  
> the CartItem class so that it can look for an instance of CartItem?

> # something really clever
> 
> end

Well if you want instances of cart\_item to persist across requests you  
will have to store them somewhere (database, session, memcache, flat  
file on your server etc etc…).  
If you mean ‘can i magically find an instance of cartitem that was  
created in an earlier request and that I haven’t stored anywhere  
persistent’ then the answer is no (well you can limp along with class  
variables, but that doesn’t work in development mode or when you scale  
past 1 mongrel).

Fre

---

<div class="post-metadata">

**Author:** ![GlennNZ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7c8e57/32.png) [@GlennNZ](https://www.ruby-forum.com/u/GlennNZ)\
**Post date:** [November 13, 2007, 12:02pm UTC](https://www.ruby-forum.com/t/how-to-retrieve-an-object-from-form-remote-tag/120022/6 "2007-11-13T12:02:41Z")

</div>

On 13 Nov 2007, at 10:40, GlennNZ wrote:

> I have a Cart object which has an Array of CartItems which in turn  
> holds a Service object which inherits from ActiveRecord. The Cart  
> object is stored in the session variable so yes, it is persistent and  
> therefore all the instances of CartItem should be persistent too.

If they’re stored in the session there’s no magic way for the CartItem  
class to find instances of itself, since models don’t have access to  
the session. presumably you could do something like  
session[:cart].find\_cart\_item(…)

Fred

---

<div class="post-metadata">

**Author:** ![GlennNZ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7c8e57/32.png) [@GlennNZ](https://www.ruby-forum.com/u/GlennNZ)\
**Post date:** [November 13, 2007, 12:00pm UTC](https://www.ruby-forum.com/t/how-to-retrieve-an-object-from-form-remote-tag/120022/7 "2007-11-13T12:00:50Z")

</div>

GlennNZ wrote:

> On Nov 13, 11:33 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > > … :id =\> cart\_item…  
> > > Well if you want instances of cart\_item to persist across requests you  
> > > Cheers  
> > > I have a Cart object which has an Array of CartItems which in turn  
> > > holds a Service object which inherits from ActiveRecord. The Cart  
> > > object is stored in the session variable so yes, it is persistent and  
> > > therefore all the instances of CartItem should be persistent too.

so if i got that right you Cart defines (or should) an association like  
this:

class Cart \< ActiveRecord::Base

has\_many :services

…

then you could do something like

class Basket \< ActiveRecord::Base

has\_many :services do  
def my\_finder  
# something real clever  
end  
end

then you can access it eg.: @cart.services.my\_finder

find details here: [http://api.rubyonrails.org/](http://api.rubyonrails.org/)  
look for “Association extensions”

---

<div class="post-metadata">

**Author:** ![GlennNZ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7c8e57/32.png) [@GlennNZ](https://www.ruby-forum.com/u/GlennNZ)\
**Post date:** [November 13, 2007, 8:46pm UTC](https://www.ruby-forum.com/t/how-to-retrieve-an-object-from-form-remote-tag/120022/8 "2007-11-13T20:46:37Z")

</div>

On Nov 14, 12:00 am, Thorsten M. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> class Basket \< ActiveRecord::Base
> 
> has\_many :services do  
> def my\_finder  
> # something real clever  
> end  
> end
> 
> then you can access it eg.: @cart.services.my\_finder

Okay, it never occurred to me to use ActiveRecord on an object that  
wasn’t actually a database table. Admittedly what I am playing around  
with is derived from the shopping cart tutorial in the Agile Web  
Development with Rails book. Currently my code works by doing the  
following

#the partial  
… :id =\> cart\_item.service.id …

#the controller  
my\_service = Service.find(params[:id])  
session[:cart].remove\_from\_cart(my\_service)

#Cart  
def remove\_from\_cart(service)  
item = @items.find{ |item| item.service == service }  
@items.delete(item)  
end

I should play around a bit more with active record. I’ve realised how  
I’ve misinterpreted what the find method is doing and therefore why I  
can’t use it for what I was trying to do originally.

Thanks for all the help, guys.
