# Ajax to redisplay

**URL:** <https://www.ruby-forum.com/t/ajax-to-redisplay/142897>\
**Category:** Rails\
**Created:** [July 12, 2008, 11:45pm UTC](https://www.ruby-forum.com/t/ajax-to-redisplay/142897 "2008-07-12T23:45:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 12, 2008, 11:45pm UTC](https://www.ruby-forum.com/t/ajax-to-redisplay/142897/1 "2008-07-12T23:45:35Z")

</div>

Hi,

I have a button that displays “Show form.” When a user clicks on it, I  
want the form to be display on the page without having to reload the  
page.

This is what I’ve got so far:

controller action #show\_add\_user

respond\_to { |format| format.js }

In my view template I have:

\<% hidden\_div\_if($a, :id =\> “add\_user\_form”) } do $\>  
\<%= render :partial =\> “form” %\>  
\<% end %\>

and in my show\_form.js.rjs:  
page.replace\_html(“add\_user\_form”, :partial =\> “form”)

This is the \_form.html.erb (partial):

\<% form\_for :user, @user, :url =\> { :action =\> “create\_user” } do |f|  
%\>

  
  
Email: \<%= f.text\_field :email %\>  
  

```
<tr>
  <td>
    Password: <%= f.text_field :password %>
  </td>
</tr>

  <tr>
    <td>
        <%= submit_tag "Add user" %>
    </td>
  </tr>

```

\<% end %\>

Anyway help is much appreciated, thanks.

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 13, 2008, 12:52am UTC](https://www.ruby-forum.com/t/ajax-to-redisplay/142897/2 "2008-07-13T00:52:10Z")

</div>

Justin To wrote:

> I have a button that displays “Show form.” When a user clicks on it, I  
> want the form to be display on the page without having to reload the  
> page.
> 
> This is what I’ve got so far:
> 
> controller action #show\_add\_user
> 
> respond\_to { |format| format.js }

That’s over the top. Put a div with id ‘add\_user’ into your page, put  
your form  
into a partial, and render it like this:

def show\_add\_user  
render :update do |rjs|  
rjs.replace\_html :add\_user, :partial =\> ‘add\_user\_form’  
end  
end

Note that my variable ‘rjs’ is usually called ‘page’. I never call it  
that  
because the world has far, far too many variables called ‘page’.

> In my view template I have:
> 
> \<% hidden\_div\_if($a, :id =\> “add\_user\_form”) } do $\>  
> \<%= render :partial =\> “form” %\>  
> \<% end %\>

I hope to eventually find a use for hidden\_div\_if. But…

> and in my show\_form.js.rjs:  
> page.replace\_html(“add\_user\_form”, :partial =\> “form”)

If the div is hidden, not non-existent, why re-render it? Why not just  
make it  
visible?

And why write a whole js file when you can just put what you actually  
need into  
a render :update, in the controller?

Now write unit tests for everything you do, using either assert\_rjs or  
assert\_javascript to constrain the Ajax.

–  
Phlip

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 13, 2008, 5:30am UTC](https://www.ruby-forum.com/t/ajax-to-redisplay/142897/3 "2008-07-13T05:30:30Z")

</div>

I added all of those goodies,

```
  <%= link_to_remote( "+ Add user",
                    :update => "add_user",
                    :url => { :action => :show_add_user }) %>

```

Is my link to invoke the action, but whenever I click it, there doesn’t  
seem to be any action!

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 13, 2008, 5:40am UTC](https://www.ruby-forum.com/t/ajax-to-redisplay/142897/4 "2008-07-13T05:40:26Z")

</div>

Ok, I resolved it, somewhat… but now when i click it, along with my  
form, it has

y { Element.update(“add\_user”, "  
\n  
\n \n Email: \n \n  
\n \n \n \n Password: \n \n  
\n \n \n \n \n \n  
\n \n"); } catch (e) { alert(‘RJS error:\n\n’ + e.toString());  
alert(‘Element.update(“add\_user”, "  
\n \n \n Email: \n \n  
\n \n \n \n Password: \n \n  
\n \n \n \n \n \n  
\n  
\n");’); throw e }

What’s the problem here??

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 13, 2008, 6:26am UTC](https://www.ruby-forum.com/t/ajax-to-redisplay/142897/5 "2008-07-13T06:26:12Z")

</div>

How would I run some effects like visual\_effect :blind\_up

for the div ‘add\_user’ ?

Thanks!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 13, 2008, 6:30am UTC](https://www.ruby-forum.com/t/ajax-to-redisplay/142897/6 "2008-07-13T06:30:08Z")

</div>

Justin To wrote:

> How would I run some effects like visual\_effect :blind\_up
> 
> for the div ‘add\_user’ ?
> 
> Thanks!

The visual effect, and also I’m lost as to make the div disappear now if  
I click on the ‘Add user’ link and the div is currently showing

Thanks!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 13, 2008, 5:41am UTC](https://www.ruby-forum.com/t/ajax-to-redisplay/142897/7 "2008-07-13T05:41:00Z")

</div>

Ha ha, I figured it out:

```
  <%= link_to_remote( "+ Add user",
                    :url => { :action => :show_add_user }) %>

```

I was updating twice.

THanks!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 13, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/ajax-to-redisplay/142897/8 "2008-07-13T16:37:24Z")

</div>

Justin To wrote:

> Is my link to invoke the action, but whenever I click it, there doesn’t  
> seem to be any action!

Install Firebug on your Firefox, and view its debugging console while  
clicking.
