Railsbridge Workshop

Contents

Other Pages

Site List

Auth Logic

Authentication

 

Register

account_path do |f| %>







filter_parameter_logging :password, :password_confirmation helper_method :current_user_session, :current_user

private

def current_user_session
  return @current_user_session if defined?(@current_user_session)
  @current_user_session = UserSession.find
end

def current_user
  return @current_user if defined?(@current_user)
  @current_user = current_user_session && current_user_session.user
end

def require_user
  unless current_user
    store_location
    flash[:notice] = "You must be logged in to access this page"
    redirect_to new_user_session_url
    return false
  end
end

def require_no_user
  if current_user
    store_location
    flash[:notice] = "You must be logged out to access this page"
    redirect_to account_url
    return false
  end
end

def redirect_back_or_default(default)
  redirect_to(session[:return_to] || default)
  session[:return_to] = nil
end

def store_location
  session[:return_to] = request.request_uri
end

end

* user controller
# config/routes.rb map.resource :account, :controller => "users" map.resources :users

* Create your UsersController:
script/generate controller users

* add to user controller
def new

@user = User.new

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @user }
end

end

# POST /users # POST /users.xml def create

@user = User.new(params[:user])

respond_to do |format|
  if @user.save
    flash[:notice] = 'You successfully registered.'
    format.html { redirect_to topics_path }
    format.xml  { render :xml => @user, :status => :created, :location => @user }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @topic.errors, :status => :unprocessable_entity }
  end
end

end
* add to UserSessionsController
class UserSessionsController < ApplicationController before_filter :require_no_user, :only => [:new, :create] before_filter :require_user, :only => :destroy

def new

@user_session = UserSession.new

end

def create

@user_session = UserSession.new(params[:user_session])
if @user_session.save
  flash[:notice] = "Login successful!"
  redirect_back_or_default account_url
else
  render :action => :new
end

end

def destroy

current_user_session.destroy
flash[:notice] = "Logout successful!"
redirect_back_or_default new_user_session_url

end end
* add to topic controller
before_filter :require_user, :only => [:new, :edit, :create, :update]

* rake db:migrate * go to: localhost:3000/account/new * and register, see that you are at topics_path and show cookies to show that the session is being tracked by a cookie * add user_sessions view new.html.erb

    

Login

user_session_path do |f| %>







Railsbridge Docs

Source: https://github.com/railsbridge/docs