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
--
Posted via http://www.ruby-forum.com/.