# New to RoR - working through book have some questions

**URL:** <https://www.ruby-forum.com/t/new-to-ror-working-through-book-have-some-questions/175796>\
**Category:** Rails\
**Created:** [September 19, 2009, 11:57pm UTC](https://www.ruby-forum.com/t/new-to-ror-working-through-book-have-some-questions/175796 "2009-09-19T23:57:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [September 19, 2009, 11:57pm UTC](https://www.ruby-forum.com/t/new-to-ror-working-through-book-have-some-questions/175796/1 "2009-09-19T23:57:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [September 20, 2009, 12:03am UTC](https://www.ruby-forum.com/t/new-to-ror-working-through-book-have-some-questions/175796/2 "2009-09-20T00:03:00Z")

</div>

On Sep 19, 8:40 pm, andrew [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
