# Using Ajax in Rails 2.1

**URL:** <https://www.ruby-forum.com/t/using-ajax-in-rails-2-1/145356>\
**Category:** Rails\
**Created:** [August 8, 2008, 11:32am UTC](https://www.ruby-forum.com/t/using-ajax-in-rails-2-1/145356 "2008-08-08T11:32:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrgra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba8739/32.png) [@chrgra](https://www.ruby-forum.com/u/chrgra)\
**Post date:** [August 8, 2008, 11:32am UTC](https://www.ruby-forum.com/t/using-ajax-in-rails-2-1/145356/1 "2008-08-08T11:32:44Z")

</div>

I hate to ask, but can some one help me out on this one? I’ve made a  
small app in which users login, and once logged in they type their  
names in to a textbox, submit it, and once submitted it says "Hello  
and Welcome ". For authentication I used  
acts\_as\_authenticated. The app works except for the adding names  
feature. Can you help me? This is the code that i put, but I can’t get  
the submitted name to appear next to “Hello and Welcome” so all  
instead of seeing "Hello and Welcome ", it shows Hello and  
Welcome.  
Here’s the code for my view

 My little login APP! \<%= javascript\_include\_tag :defaults %\> 
# My little login APP!

```
<%= error_messages_for :user %>
<% form_for :name do |f| %>

<%= form_remote_tag (:update => "name",
                    :url => { :action => :add_item},
                    :position => "top") %>

<p>Please submit your name:</p>
<%= text_field_tag :newitem %>
<%= submit_tag "Submit" %>

```

### Hello and Welcome!

```
<hr>
</hr>

<p><b><i><%= current_user.login %></p></b></i>
<p><a href="account/login"><%= submit_tag 'Logout' %></a></p>
<% end %>

```

---

<div class="post-metadata">

**Author:** ![chrgra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba8739/32.png) [@chrgra](https://www.ruby-forum.com/u/chrgra)\
**Post date:** [August 8, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/using-ajax-in-rails-2-1/145356/2 "2008-08-08T14:36:15Z")

</div>

That’s because you forgot to surround fields that need to be submitted  
in ajax call in do…end block. Also if i remember correctly html  
doesn’t allow nesting forms, so your code should be rewritten like  
this:  
\<% form\_remote\_tag (:update =\> “name”,  
:url =\> { :action =\> :add\_item},  
:position =\> “top”) do %\>

```
<p>Please submit your name:</p>
<%= text_field_tag :newitem %>
<%= submit_tag "Submit" %>

```

\<% end %\>

\<% form\_for :name do |f| %\>

### Hello and Welcome!

```
<hr>
</hr>

<p><b><i><%= current_user.login %></p></b></i>
<p><a href="account/login"><%= submit_tag 'Logout' %></a></p>

```

\<% end %\>

Notice do…end near remote form tag. Also your html is not really  
proper. You should first close tags that were opened last. So instead  
of

### Hello and Welcome!
 you should write 

### Hello and Welcome!
. Also there’s no need to use h3 and p tags  
together since h3 is a paragraph header. And ul should be used  
together with li. I believe in your case div would be enough.

---

<div class="post-metadata">

**Author:** ![chrgra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba8739/32.png) [@chrgra](https://www.ruby-forum.com/u/chrgra)\
**Post date:** [August 8, 2008, 5:39pm UTC](https://www.ruby-forum.com/t/using-ajax-in-rails-2-1/145356/3 "2008-08-08T17:39:41Z")

</div>

On 8 Aug 2008, at 09:54, chrgra wrote:

> Here’s the code for my view  
> The interesting bit is probably what’s in your controller.  
> Fred
