Need an explanation to why this is

I am following the examples in the Agile Web D. with Rails
book and there is one thing that I dont quite understand.

If you aren’t familiar with the book a lot of it is geared towards
making a shopping site. The point that I am at is tying in AJAX with
the updating of the contents of the users shopping cart.

This is the peice of code within the store.rhtml file

<%= render :partial => 'cart', :object => @cart %>

and this is the code within the _cart.rhtml file

Your Cart

<%= render :partial => "cart_item", :collection => cart.items %>
Total: <%= number_to_currency(cart.total_price) %>

<%= button_to ‘Empty Cart’, :action => :empty_cart %>


and this is the code within the add_to_cart.rjs file

page[:cart].replace_html :partial => “cart”, :object => @cart

/////////////////////

Now after going through the example and getting everything working I
skimmed over it one more time and wondered if things would work if I
never created the partial for the cart. So I substituted the code
within the _cart.rhtml file into the “cart” div tag in the main rhtml
file and it worked. actually there was an initial error in that where
I referenced the “cart” I did so by @cart, this was done by an initial
mistake which is why it worked. And this I can understand since the
@cart var was created in the index method within the controller.

What I don’t understand is what is happening in the rjs file.

page[:cart].replace_html :partial => “cart”, :object => @cart

When I got everything working after making the changes I thought that I
should be able to remove the :partial => cart section of the code
above, since the code that previously existed within the partial file
was moved to the store.rhtml file so there is no partial that is
allowing the cart to be displayed. Now for some reason it is at this
point that it blows up and throws the errors on to the screen.

It is getting late so I am not sure if I explained that clearly or not,
but I understand why it worked after moving the code from the partial
to the main page, but not why the removal of the :partial section from
the page[:cart].replace_html method failed. Is it as simple as
replace_html requires a partial? I can’t find any info on the
render_html method in the ruby docs at http://api.rubyonrails.org/ so I
could use a little input from the pros out there :slight_smile:

Thanks for the help

chris wrote:


page[:cart].replace_html :partial => “cart”, :object => @cart

When I got everything working after making the changes I thought that I
should be able to remove the :partial => cart section of the code
above, since the code that previously existed within the partial file
was moved to the store.rhtml file so there is no partial that is
allowing the cart to be displayed. Now for some reason it is at this
point that it blows up and throws the errors on to the screen.

The point of using AJAX here is that instead of refreshing the entire
page when you add something to your cart, you can just update the part
of the page that displays the shopping cart. The RJS line above is
saying “find the div called ‘cart’ in the current page, and replace it
with the contents of the partial template called ‘cart’”.

That’s why you have the ‘shopping cart’ bit of RHTML in a separate file
called _cart.rhtml. It’s so that you can reference just that bit of
HTML. If you lump the shopping cart HTML in with the overall page, how
will you tell Rails which bit of RHTML you want to re-render?

It sounds like you were expecting ‘replace_html’ to look through your
main RHTML file, and pull out the code that was originally rendered
inside the ‘cart’ div, and re-render that. I’m afraid it’s not that
smart!

When you call ‘replace_html’, all it’s doing is rendering some RHTML in
the usual way, and then using Javascript in the browser to replace a
specified part of the current page with the newly rendered RHTML. You
could tell it to render a completely different partial template if you
wanted, or you could tell it just to render some text. That’s why you
have to specify a partial template in this case – if you didn’t, it
would have no way of knowing what you’re expecting it to render.

Chris

Thanks for the reply. it makes sense I guess although I am not sure if
it is that complex to replace the contents of a div tag with an id.

It is hard to tell how much information was being posted back from the
server and whether more information was being posted back doing it the
second way, although maybe if I watched the cmd window that the WEBrick
server is running in it would answer that.

Thanks for the input :slight_smile: