Basic sign up and logging in Routes issue

Hey everyone,

Sorry for the novice question but I am trying to get to grips with RoR. I have a very basic sign up and login process in place but am having some difficulty getting the routing correct. I am also unsure whether I am actually being logged out successfully when I push my logout button because it isn’t then displaying the login button as it should.

My setup is as so:

Sessions Controller

class SessionsController < ApplicationController
	def new
	end
	
	def create
		user = User.find_by_email(params[:email])
		if user && user.authenticate(params[:password])
			session[:user_id] = user.id
			redirect_to root_url, :notice => "Logged in!"
		else
			flash.now.alert = "Invalid email or password!"
			render "signup"
		end
	end
	
	def destroy
		session[:user_id] = nil
		redirect_to root_url, :notice => "Logged Out!"
	end
	
end

User Controller

class UserController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new (params[:user])
      if @user.save
        redirect_to root_url, :notice => "Signed Up!"
      else
        render "user/new"
    end
  end
end

User Model

class User < ActiveRecord::Base
	has_secure_password
	validates_confirmation_of :password
	validates_presence_of :password, :on => :create
	validates_presence_of :email
	validates_uniqueness_of :email, :on => :create
	
end

Sessions/new.html.erb

<h1>Log In</h1>

<%= form_tag login_path do %>
	<div class="field">
		<%= label_tag :email %>
		<%= text_field_tag :email, params[:email] %>
	</div>
	
	<div class ="field">
		<%= label_tag :password %>
		<%= password_field_tag :password %>
	</div>
	
	<div class="actions"><%= submit_tag "Log in" %></div>
<%end%>

User/new.html.erb

<% if session[:user_id] %>
  <!-- user is logged in -->
  <%= link_to logout_path %>
<% else %>
  <!-- user is not logged in -->
  <%= link_to login_path %>
<% end %>

<h1>Sign Up</h1>

<%= form_for @user do |f| %>
	<% if @user.errors.any? %>
		<div class="error_messages">
		 <h2>Form is invalid</h2>
			<ul>
				<% for message in @user.errors.full_messages %>
					<li><%= message %></li>
				<% end %>
			</ul>
		</div>
	<% end %>
	
	<div class = "field">
		<%= f.label :email %>
		<%= f.text_field :email %>
	</div>
	
	<div class = "field">
		<%= f.label :password %>
		<%= f.password_field :password %>
	</div>
	
	<div class = "field">
		<%= f.label :password_confirmation %>
		<%= f.password_field :password_confirmation %>
	</div>
	
	<div class="actions"><%= f.submit %></div>
<% end %>	

Finally my Routes file

MadeByV2::Application.routes.draw do

    controller :user do
    get "signup" => "user#new"
    end

    resources :users

    controller :sessions do
      get "login" => "sessions#new"
      post "login" => "sessions#create"
      delete "logout" => "sessions#destroy"
    end

  root :to => "user#new"

end

Sorry for the extensive use of code in this post but I figure it’s best to give a well rounded view of everything so people can see where I am going wrong.

Any help you can offer really would be much appreciated because I don’t seem to be getting it myself :slight_smile:

Thanks,
Tom

You should write a helper method to determine if the user is currently logged in. Something like…


def current_user 
  @current_user ||= User.find(session[:user_id]) if session[:user_id]
end

Then you can use it in your views like this:


<div id="user_nav">
  <% if current_user %>
    Logged in as <%= current_user.email %>.
    <%= link_to "Log out", log_out_path %>
  <% else %>
    <%= link_to "Sign up", sign_up_path %> or
    <%= link_to "log in", log_in_path %>
  <% end %>
</div>

Your routes could look something like this:


get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
root :to => "users#new"
resources :users
resources :sessions

Those two things should get you going in the right direction. I also recommend that you check out the Railscasts episode about authentication from scratch. It gives a good overview of how to get started.

Hi Scannon,

Thanks for your answer on this! I have been going through a few of these railcasts but find they often have small errors in that causes errors to be thrown up.

I managed to get :user_id to work in the end but having now added a :auth_token method instead to validate authentication through cookies, am struggling to get the same Login/Logout element to load correctly.

I currently have:

<% if session[:current_user] %>
  <!-- user is logged in -->
  <%= link_to logout_path %>
<% else %>
  <!-- user is not logged in -->
  <%= link_to login_path %>
<% end %>

but even though I have created a helper as you suggested I can’t seem to get it to work.

Cheers,
Tom

Scannon’s codes are of great help to me. Use them so you have your problem solved.