# AJAX Preview & Edit Page Problem

**URL:** <https://www.ruby-forum.com/t/ajax-preview-edit-page-problem/96561>\
**Category:** Rails\
**Created:** [January 24, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/ajax-preview-edit-page-problem/96561 "2006-01-24T15:40:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![boram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7993a0/32.png) [@boram](https://www.ruby-forum.com/u/boram)\
**Post date:** [January 24, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/ajax-preview-edit-page-problem/96561/1 "2006-01-24T15:40:53Z")

</div>

I’m new to RoR and have started building my first application.

I’m building an account sign-up controller and I have two questions:

1. What is the best way to pass form params between methods in my  
sign-up controller? The solution I’m using seems too hacky and there’s  
got to be a something more elegant. I’m instantiating User.new to  
capture the form field params, then storing them in session[] to make  
these values available for editing in the current session (see code  
below).

2. Is there a way to change the same AJAX update div multiple times?  
Specifically, I would like to do the following:

a. Show a form in div  
b. User populates and submits form  
c. Controller shows preview in the same div  
d. User chooses to edit form  
e. Show form again for editing with fields populated with previously  
entered params

Note that AJAX form is to be replaced with the preview page (I want the  
preview page to have different buttons and no form).

## MAIN .RHTML PAGE

 \<%= form\_remote\_tag(:update =\> "signup\_form", :url =\> {:action =\> "signup"}) %\> \<%= render(:partial =\> "user/edit") %\>

```
    <%= hidden_field_tag('signup_button', 'Preview') %>
    <td><%= submit_tag "Preview", :name => "_signup_button" %></td>
    <td><%= submit_tag "Cancel", :name => "_signup_button",
     		:onclick => "Form.getInputs(this.form, null, 

```

‘signup\_button’)[0].value = ‘Cancel’" %\>  
\<%= end\_form\_tag %\>

## FORM EDIT PAGE -\> rendered in main page, replaced by PREVIEW PAGE, then I’d like to put it back in the update div

| Username | \<%= text\_field(:user, :name, :size =\> 20) %\> |
| Password | \<%= password\_field(:user, :password, :size =\> 20) %\> |
| email | \<%= text\_field(:user, :email, :size =\> 20) %\> |
| Phone | \<%= text\_field(:user, :phone, :size =\> 20) %\> |
| Hide phone number? | \<%= check\_box(:user, :phone\_hide) %\> |

## PREVIEW PAGE

### Is this information correct?
 \<% session[:context] = "signup\_edit" %\> 

| User name: | \<%= session[:name] %\> |
| email: | \<%= session[:email] %\> |
| Phone: | \<%= session[:phone] %\> |
| Hide phone: | \<%= session[:phone\_hide] %\> |
| \<%= button\_to "Edit", :action =\> "edit\_signup" %\> | \<%= button\_to "Confirm", :action =\> "commit\_signup" %\> |

## CONTROLLER

def signup

if request.get?  
if session[:context] == “signup\_edit”  
@user = User.new  
@user.name = session[:name]  
@user.phone = session[:phone]  
@user.email = session[:email]  
@user.phone\_hide = session[:phone\_hide]  
@user.hashed\_password = session[:hashed\_password]  
session[:context] = “signup”  
else  
@user = User.new  
session[:context] = “signup”  
end  
else  
if params[:signup\_button] = “Preview”  
@user = User.new(params[:user])  
session[:name] = @user.name  
session[:phone] = @user.phone  
session[:email] = @user.email  
session[:phone\_hide] = @user.phone\_hide  
session[:hashed\_password] = @user.hashed\_password  
render(:partial =\> “preview\_signup”)  
else  
#Cancel  
end  
end

end

def edit\_signup  
session[:context] = “signup\_edit”  
redirect\_to(:action=\>“signup”)  
end

---

<div class="post-metadata">

**Author:** ![boram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7993a0/32.png) [@boram](https://www.ruby-forum.com/u/boram)\
**Post date:** [January 24, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/ajax-preview-edit-page-problem/96561/2 "2006-01-24T18:05:03Z")

</div>

Boram,

There’s no need to re-render your form if it’s not changing.  
Just hide it on preview, and show it again on edit.  
You’ll have to use javascript to set a hidden form parameter that stores  
whether you’re previewing or submitting,  
as ajax forms won’t recognize which button was pressed.

joshua

a. Show a form in div

---

<div class="post-metadata">

**Author:** ![boram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7993a0/32.png) [@boram](https://www.ruby-forum.com/u/boram)\
**Post date:** [January 27, 2006, 5:09am UTC](https://www.ruby-forum.com/t/ajax-preview-edit-page-problem/96561/3 "2006-01-27T05:09:10Z")

</div>

Thanks for responding, Joshua.

Using Javascript to collapse the div is certainly faster than doing  
XMLHttpRequest, but I want to do form validation as well. I realize  
form validation can also be done in Javascript on the client side, but  
isn’t it better to keep that stuff in the controller?

Joshua B. wrote:

> Boram,
> 
> There’s no need to re-render your form if it’s not changing.  
> Just hide it on preview, and show it again on edit.  
> You’ll have to use javascript to set a hidden form parameter that stores  
> whether you’re previewing or submitting,  
> as ajax forms won’t recognize which button was pressed.
> 
> joshua
> 
> a. Show a form in div

---

<div class="post-metadata">

**Author:** ![boram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7993a0/32.png) [@boram](https://www.ruby-forum.com/u/boram)\
**Post date:** [April 4, 2006, 3:23pm UTC](https://www.ruby-forum.com/t/ajax-preview-edit-page-problem/96561/4 "2006-04-04T15:23:32Z")

</div>

On 1/27/06, Boram [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for responding, Joshua.
> 
> Using Javascript to collapse the div is certainly faster than doing  
> XMLHttpRequest, but I want to do form validation as well. I realize  
> form validation can also be done in Javascript on the client side, but  
> isn’t it better to keep that stuff in the controller?

Yes, I agree that that’s indeed better. If you want to know how to do  
form  
validation in your controller and still use AJAX request, you may want  
to  
read [AJAX validation on Rails](http://www.bigsmoke.us/ajax-validation-in-rails), an article in  
which I  
describe a solution to these and related problems.

Please let me know if you find anything incomplete, unclear or missing,

- Rowan
