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…
…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:
<%= 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.
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)
… :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.
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).
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(…)
… :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
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
#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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.