Using Ajax in Rails 2.1

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 %>

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.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs