Combined sign in and registration page?
Posted
by
Ryan
on Stack Overflow
See other posts from Stack Overflow
or by Ryan
Published on 2011-03-02T23:22:32Z
Indexed on
2011/03/02
23:24 UTC
Read the original article
Hit count: 261
ruby-on-rails
|ruby-on-rails-3
This is somewhat against rails convention but I am trying to have one controller that manages both user session authentication and user registration. I am having troubles figuring out how to go about this.
So far I am merging the User Controller and the Sessions Controller and having the 'new' method deliver both a new usersession and a new user instance.
With the new routes in rails 3 though, I am having trouble figuring out how to generate forms for these items.
Below is the code:
user_controller.rb
class UserController < ApplicationController
def new
@user_session = UserSession.new
@user = User.new
end
def create_user
@user = User.new(params[:user])
if @user.save
flash[:notice] = "Account Successfully Registered"
redirect_back_or_default signup_path
else
render :action => new
end
end
def create_session
@user_session = UserSession.new(params[:user_session])
if @user_session.save
flash[:notice] = "Login successful!"
redirect_back_or_default login_path
else
render :action => new
end
end
end
views/user/new.html.erb
<div id="login_section">
<% form_for @user_session do |f| -%>
<%= f.label :email_address, "Email Address" %>
<%= f.text_field :email %>
<%= f.label :password, "Password" %>
<%= f.text_field :password %>
<%= f.submit "Login", :disable_with => 'Logining...' %>
<% end -%>
</div>
<div id="registration_section">
<% form_for @user do |f| -%>
<%= f.label :email_address, "Email Address" %>
<%= f.text_field :email %>
<%= f.label :password, "Password" %>
<%= f.text_field :password %>
<%= f.label :password_confirmation, "Password Confirmation" %>
<%= f.text_field :password_confirmation %>
<%= f.submit "Register", :disable_with => 'Logining...' %>
<% end -%>
</div>
I imagine I will need to use :url => something for those forms, but I am unsure how to specify.
Within routes.rb I have yet to specify either Usersor UserSessions as resources (not convinced that this is the best way to do it... but I could be).
I would like, however, the registration and login on the same page and have implemented this by doing the following:
routes.rb
match 'signup' => 'user#new'
match 'login' => 'user#new'
What's the best way to go about solving this?
© Stack Overflow or respective owner