11175
(-- --)
1
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.
Phlip
(Phlip)
2
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.
11175
(-- --)
3
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.
11175
(-- --)
4
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??
11175
(-- --)
5
Ha ha, I figured it out:
<%= link_to_remote( "+ Add user",
:url => { :action => :show_add_user }) %>
I was updating twice.
THanks!
11175
(-- --)
6
How would I run some effects like visual_effect :blind_up
for the div 'add_user' ?
Thanks!
11175
(-- --)
7
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!
Phlip
(Phlip)
8
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.