1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Rep Power

    How to retrieve an object from form_remote_tag?

    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
    ...but neither work so obviously I've completely misinterpreted rails again. I get an error like.
    NoMethodError (undefined method `name' for "#<CartItem:0x47dd9f4>":String):
    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.
    Last edited by GlennNZ; November 12th, 2007 at 06:57 PM.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Woodland Hills, Los Angeles County, California, USA
    Rep Power
    Perhaps what you're passing to the partial is not a CartItem object. You could be passing some other object in with the variable name cart_item (Notice the case and the spelling). For instance:
       product = Product.find(id)
       render :partial => 'partial_template', :locals => { :cart_item => product, :header => 'hello' }
    In this case, what is being passed to cart_item is an instance of an object Product, which may actually have a name method. Hence, when your partial does:
    <%= cart_item.name %>
    it then works correctly.

    One quick way to verify if you're passing a different object to the partial is by doing this:
    <%= cart_item.inspect %>
    which should tell you what the object is.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo