ajax to redisplay

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|
%>
    <tr>
      <td>
        Email: <%= f.text_field :email %>
      </td>
    </tr>

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

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

Anyway help is much appreciated, thanks.

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.

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.

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

Ha ha, I figured it out:

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

I was updating twice.

THanks!

How would I run some effects like visual_effect :blind_up

for the div 'add_user' ?

Thanks!

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!

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.