New to RoR - working through book have some questions

Greetings,

I’m new to Ruby on Rails. I’m working through “Agile Web D.
with Rails 3rd Edition” and it is going quite well. However, there
are times I don’t quite understand why I’m doing something. For
some items, I think I understand, but I would like some confirmation.
So, I’m going to post my questions here and I hope that someone can
help me in understanding.

In Chapter 9, I’m working on creating an AJAX-based shopping cart.

So, I’m replacing this:

<%= button_to “Add to Cart”, :action => ‘add_to_cart’, :id => product
%>

With this:

<% form_remote_tag :url => { :action => ‘add_to_cart’, :id =>
product } do %>
<%= submit_tag “Add to Cart” %>
<% end %>

Questions on this:

I can explain that the first second is doing this: “Use ruby helper
button_to to add a button for each item, and it calls the action
add_to_cart, passing back the product that was selected”

I can’t seem to be able to explain the second section. “The rails
helper form_remote_tag is called, which is similar to a method call
and the :url is similar to passing a parameter to a method. The
items between { and } are the values for the :url parameter.”
Correct?

What is the “submit_tag”? Is that similar to the input type=“submit”
in HTML? Is it basically saying, show a button?

Why is the do%> and <% end %> part needed? Is this saying do the
following ruby code?

The difference between the two seconds, as the book explains, is that
the second sends an AJAX request instead of a POST request. I read
that, I think I understand it, but I don’t exactly know why there is a
difference. Can someone explain?

I’ll just start with this. Thanks,

Andrew

On Sep 19, 8:40 pm, andrew [email protected] wrote:

<% form_remote_tag :url => { :action => ‘add_to_cart’, :id =>
I can’t seem to be able to explain the second section. “The rails
helper form_remote_tag is called, which is similar to a method call
and the :url is similar to passing a parameter to a method. The
items between { and } are the values for the :url parameter.”
Correct?

pretty much. What’s in the :url hash are routing options, ie the same
thing as the second parameter to button_to or link_to

What is the “submit_tag”? Is that similar to the input type=“submit”
in HTML? Is it basically saying, show a button?

the xxx_tag helpers generate the appropriate input tags, so submit tag
is input type=submit, textfield_tag is a textfield etc.

Why is the do%> and <% end %> part needed? Is this saying do the
following ruby code?

form_tag / remote_form_tag take a block - the do/end are just the
block delimiters

The difference between the two seconds, as the book explains, is that
the second sends an AJAX request instead of a POST request. I read
that, I think I understand it, but I don’t exactly know why there is a
difference. Can someone explain?

AJAX is a big topic in itself but the key difference is that whereas a
‘normal’ request reloads the whole page, an AJAX request just happens
in the background and you get to decide how the response from the
server is used.

Fred