Quick question with restful_authentication and validations

Hi everyone,

I’m using restful_authentication for my app - using Rails 2.0.2. I
have things configured properly, and I’m using the “shortcut” named
route of signup in my routes.rb like so:

map.signup “/signup”, :controller => “users”, :action => “new”

My /views/users/new.html.erb file has the form set up like this
(unchanged from restful_authentication):

<% form_for :user, :url => users_path do |f| -%>

Username
<%= f.text_field :login %> <%= error_message_on :user, :login, "↑ Username", " is required"%>

Email
<%= f.text_field :email %> <%= error_message_on :user, :email, "A valid email address", " is required" -%>

Password
<%= f.password_field :password %>
<%= error_message_on :user, :password, "A password is required" -%>

Confirm Password
<%= f.password_field :password_confirmation %>
<%= error_message_on :user, :password_confirmation, "Passwords must match" -%>

<%= submit_tag 'Sign up' %>

<% end -%>

Everything works, but there’s a stylistic problem - To go to the
signup page, the user goes to http://myapp/signup. If validation
fails, though, when the page reloads to show validation errors, the
address bar reads http://myapp/users, and if the user accidently hits
Return/Enter it will give an error because there is no index action in
the UsersController (nor do I want one - UsersController is only
going to deal with signing up new users). Is there any way to make it
stay as http://myapp/signup? I’ve tried changing the :url parameter
of the form to signup_url, but this doesn’t work because it just
reloads the form without actually invoking the create method (which
makes sense because the signup route points to the new action, not to
create).

I’ve done a Google search but no avail. I’m still a little new to
Rails and, honestly, to RESTful concepts as well but I am trying to
learn. Any help solving this problem would be appreciated.

Also, does anyone know how to override the error_message_on method so
it won’t return a DIV tag? I want my validations to appear next to
the textbox in question, not below them. Changing the CSS property of
the formError DIV to display: inline doesn’t do anything - the new DIV
is still placed below the offending textbox. This isn’t really a big
deal, as I can modify the layout to accommodate it. The address bar
issue is a bigger problem because it’s user-facing and confusing.

Wayne M wrote:

makes sense because the signup route points to the new action, not to
create).

The URLs and process flow in your web app are handled by the routes.rb
file and the controllers. In this case, you have the route defined, you
just need to make sure the controller is using it consistently. I have
a basic restful_authentication web app and this is the “create” method
from my users_controller.rb file:

def create
cookies.delete :auth_token
@user = User.new(params[:user])
@user.register! if @user.valid?
if @user.errors.empty?
self.current_user = @user
flash[:notice] = “Thanks for signing up!”
redirect_back_or_default(’/’)
else
redirect_to signup_path;
end
end

In short:

  1. Attempt to create new user after checking that form data is valid.
  2. If there are no errors, create the user.
  3. Otherwise, redirect_to signup_path

“signup_path” is defined by the rails framework when you set up the
“/signup” shortcut in your routes.rb file.

  • Aaron