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
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
Thanks for the help