small medium large xlarge

Generic-user-small
31 Jul 2010, 22:17
Alexandre de Oliveira (3 posts)

Hello. Congratulations for this great book. I am learning a lot.

However, I have a doubt. I am on page 144 (b6), creating the Ajax cart. The code that is confusing me is the following:

<%= render @cart %>

I understand the render command. According to the guide, the argument can be a string or a symbol (like render :new). However, we need to setup this @cart variable, which doesn’t exist.

@cart = current_cart

This last line, according to my understanding, returns a model instance (Cart.create or Cart.find in Application#current_cart). The argument ‘@cart’ is neither a symbol nor a string. How does ‘render @a_model_variable’ behave?

I am guessing that since @cart is Cart.create, the render command will render ‘create’ action from the ‘Cart’ controller. That makes sense, but what about the moment it returns Cart.find?

Thanks in advance for the response.

Samr_small_pragsmall
01 Aug 2010, 00:29
Sam Ruby (634 posts)

I’ll add text to clear this up. Meanwhile, you are correct that by the time the render statement is called, @cart has the value of a model instance, in particular, it will be an instance of a class named “Cart”.

render @cart is shorthand for render :partial => @cart, which in turn is short hand for render :partial => "cart", :object => @cart.

The net result is that app/views/carts/_cart.html.erb partial will be rendered, with the value of the cart variable set to the value that current_cart previously returned.

Generic-user-small
01 Aug 2010, 01:10
Alexandre de Oliveira (3 posts)

Now I got it. It’s all about the class name.

Thank you.

You must be logged in to comment