While walking through AWDWR, Task D: Add a Dash of AJAX, I was
initially unable to let the AJAX magic work. Hitting the Add to Cart
button doesn't yield to any output on its own; I had to manually
refresh the pages to witness the change. But when I tried to restart
the ruby server, and load the application again, everything worked
fine.
Any explanations to behavior mentioned above? Are there other
instances in which you have to restart a ruby server?
Well you definitely need to restart the server if you make changes to environment.rb (and its friends like things in config/initializers and the per-environment files like development.rb), routes.rb and if you install change any plugins. Without knowing exactly what was happening its hard to say in your case.
Don't know what exactly happened in that chapter/task D that you
mention.
But as a general rule you have to restart the server whenever you make
changes to the config files or include a new plugin.
Thanks for the tips, I'll keep those in mind. But I apparently did not
alter any configuration files while coding the cart application, so
its strikes me as odd that I had to restart the server in order to get
my application working well. I was unclear before, so let me try to
explain my situation more in detail.
I was working on section 9.2 of the book. It's the part wherein you
modify the "Add to Cart" button to send an AJAX request instead of a
normal HTTP Post request, by adding this code to the index.rhtml view:
<% form_remote_tag :url => { :action => :add_to_cart, :id => product }
do %>
<%= submit_tag "Add to Cart" %>
<% end %>
Then I followed these steps:
--> I enabled javascript libraries through the <%=
javascript_include_tag :defaults %>
--> Then I disabled the browser refresh upon clicking the "Add to
Cart" button by stopping the page from redirecting to the index
display
--> Afterwards, I added app/views/store/add_to_cart.rjs with this
code "page.replace_html("cart" , :partial => "cart" , :object =>
@cart)".
--> I saved everything, hit the refresh button on the browser, then
when i finally click on the "Add to Cart" button, nothing happens. The
cart display on the side bar ought to display the product that I've
chosen to add. Only after refreshing again do i see the cart updated
with the correct information. Of course, everything would eventually
work fine if i restart the server. But still, I wish to understand
this behavior to further understand the rails framework. Thanks!
And by the way, the development.log did report an error that shows
something like this:
Processing StoreController#add_to_cart (for 127.0.0.1 at 2008-05-18
22:53:41) [POST]
Session ID: 8f38baa3f8b1d03e8cc8716aec52bb3f
Parameters: {"commit"=>"Add to Cart", "action"=>"add_to_cart",
"id"=>"2", "controller"=>"store"}
e[4;36;1mProduct Columns (0.016000)e[0m e[0;1mSHOW FIELDS FROM
productse[0m
e[4;35;1mProduct Load (0.015000)e[0m e[0mSELECT * FROM products
WHERE (products.id = 2) e[0m
Rendering within layouts/store
Rendering store/add_to_cart
ActionView::TemplateError (No such file or directory - ./script/../
config/../app/views/store/add_to_cart.rhtml) in app/views/store/
add_to_cart.rhtml:
That’s very unusual to have to restart your server for it to detect a new view file, especially if there was an action for it. I was going to point out that you weren’t using the Rails 2.0 file extensions too, before I realised you’re not using Rails 2.0.
Actually I've run into this issue before also, seems to be limited to
js views used for ajax, where rails doesn't seem to know its there
until i restart the server. I have no idea why, and I have no idea
under what conditions it happens, just wanted to say I've experienced
it..
I see, my old version of rails is the culprit then. Thanks for that
info. I didn't download the latest version of rails before because I
thought that it might have deprecated some of the Rails 1.2 syntax
used in the book. I guess I should just give it a try then.