How to retrieve an object from form_remote_tag?

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 is pretty quite and railsforum seems quite young.

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 is pretty quite and railsforum seems quite young.


Sincerely,

William P.

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

On Nov 13, 11:33 pm, Frederick C. [email protected]
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.

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

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

GlennNZ wrote:

On Nov 13, 11:33 pm, Frederick C. [email protected]
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/
look for “Association extensions”

On Nov 14, 12:00 am, Thorsten M. <rails-mailing-l…@andreas-
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.