Forum: Ruby on Rails AJAX Preview & Edit Page Problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
mofo m. (Guest)
on 2006-01-24 16:40
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
----------------

<div id="signup_form">
    <%= 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'" %></td>
    <%= end_form_tag %>
</div>


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

<table>
    <tr>
        <td>Username</td>
        <td><%= text_field(:user, :name, :size => 20) %></td>
    </tr>
    <tr>
        <td>Password</td>
        <td><%= password_field(:user, :password, :size => 20) %></td>
    </tr>
    <tr>
        <td>email</td>
        <td><%= text_field(:user, :email, :size => 20) %></td>
    </tr>
    <tr>
        <td>Phone</td>
        <td><%= text_field(:user, :phone, :size => 20) %></td>
    </tr>
    <tr>
        <td>Hide phone number?</td>
        <td><%= check_box(:user, :phone_hide) %></td>
    </tr>
</table>

PREVIEW PAGE
------------

<h3>Is this information correct?</h3>
<table>
    <tr>
        <td>User name:</td><td><%= session[:name] %></td>
    </tr>
    <tr>
        <td>email:</td><td><%= session[:email] %></td>
    </tr>
    <tr>
        <td>Phone:</td><td><%= session[:phone] %></td>
    </tr>
    <tr>
        <td>Hide phone:</td><td><%= session[:phone_hide] %></td>
    </tr>
    <tr>
    	<% session[:context] = "signup_edit" %>
    	<td><%= button_to "Edit", :action => "edit_signup" %></td>
    	<td><%= button_to "Confirm", :action => "commit_signup" %></td>
    </tr>
</table>

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
Joshua B. (Guest)
on 2006-01-24 19:05
(Received via mailing list)
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
Boram (Guest)
on 2006-01-27 06:09
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
BigSmoke (Guest)
on 2006-04-04 17:23
(Received via mailing list)
On 1/27/06, Boram <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 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
This topic is locked and can not be replied to.