# Page.replace\_html problem

**URL:** <https://www.ruby-forum.com/t/page-replace-html-problem/105694>\
**Category:** Rails\
**Created:** [June 1, 2007, 2:22pm UTC](https://www.ruby-forum.com/t/page-replace-html-problem/105694 "2007-06-01T14:22:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![emiliano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3be4f8/32.png) [@emiliano](https://www.ruby-forum.com/u/emiliano)\
**Post date:** [June 1, 2007, 2:22pm UTC](https://www.ruby-forum.com/t/page-replace-html-problem/105694/1 "2007-06-01T14:22:56Z")

</div>

Hi all,  
I tried to use ajax in a simple eshop application, but the  
page.replace\_html fail in strange mode.

Controller code:

class mycontroller \< ApplicationController

# Some code

def login  
# Check user existence in db

```
  # if the user exist replace the login form with the user menu'

```

class mycontroller \< ApplicationController

# Some code

def login  
# Check user existence in db

```
  # if the user exist replace the login form with the user menu'
  if session[:user] != nil
      create_user_menu
  end

```

end

def create\_user\_menu  
@user = session[:user]

```
  render :update do |page|
     page.replace_html "login", :partial => 'user_menu', :locals

```

=\> { :user =\> @user }  
page.visual\_effect :highlight, “login”  
end  
end  
end

Layout code:

 \<%= render :partial =\> 'login' %\> 

The partial \_login.rhtml code:

\<%= start\_form\_tag({:action =\> ‘login’}, {:name =\> ‘form\_login’}) %\>  
\<%= render :partial =\> ‘login\_form’ %\>

  
\<%= link\_to\_function(“Login”, “document.form\_login.submit()”)  
%\>  

  
\<%= end\_form\_tag %\>

The partial \_user\_menu.rhtml contain only a simple html table.  
When i click on “Login” link, the browser show a new page with  
my\_controller/login action and the code generted by the js helper,  
instead the correct user menu in the page my\_controller.  
Where is the error?

Thanks in advance, Emiliano.

---

<div class="post-metadata">

**Author:** ![emiliano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3be4f8/32.png) [@emiliano](https://www.ruby-forum.com/u/emiliano)\
**Post date:** [June 1, 2007, 2:52pm UTC](https://www.ruby-forum.com/t/page-replace-html-problem/105694/2 "2007-06-01T14:52:00Z")

</div>

do you have this line somewhere in your layout header ?

\<%= javascript\_include\_tag :defaults %\>

fo the ajax stuff you need prototype and its friends

---

<div class="post-metadata">

**Author:** ![emiliano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3be4f8/32.png) [@emiliano](https://www.ruby-forum.com/u/emiliano)\
**Post date:** [June 1, 2007, 3:00pm UTC](https://www.ruby-forum.com/t/page-replace-html-problem/105694/3 "2007-06-01T15:00:00Z")

</div>

You use a normal form to send the login form’s content. this launched  
a full request cycle and results in a complete page being loaded.  
so the browser expects your server to return a complete page, while  
your server only returns a bit of javascript and the partial, which  
should of course only be done when the request was an AJAX request.

You have to use remote\_form\_for or form\_remote\_tag to send the form  
data via AJAX.See the API for Documentation.  
[http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000529](http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000529)  
[http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000530](http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000530)

---

<div class="post-metadata">

**Author:** ![emiliano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3be4f8/32.png) [@emiliano](https://www.ruby-forum.com/u/emiliano)\
**Post date:** [June 1, 2007, 5:50pm UTC](https://www.ruby-forum.com/t/page-replace-html-problem/105694/4 "2007-06-01T17:50:33Z")

</div>

ha scritto:

> 

Yes! This is the solution.

Many thanks.
