AJAX Preview & Edit Page Problem

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

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

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

On 1/27/06, Boram [email protected] 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, 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