users en sessies

This commit is contained in:
MatsMyncke 2014-11-06 18:30:53 +01:00
parent 44185e8552
commit 806dc1d8f7
17 changed files with 158 additions and 7 deletions

View file

@ -12,5 +12,6 @@
//
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require_tree .

View file

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

View file

@ -0,0 +1,3 @@
// Place all the styles related to the Sessions controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View file

@ -2,4 +2,5 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
include SessionsHelper
end

View file

@ -0,0 +1,19 @@
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(name: params[:session][:name])
if user
log_in user
redirect_to user
else
#flash.now[:danger] = 'Invalid username'
render 'new'
end
end
def destroy
end
end

View file

@ -8,4 +8,8 @@ class StaticPagesController < ApplicationController
def order
end
def help
end
end

View file

@ -1,8 +1,26 @@
class UsersController < ApplicationController
def new
@user = User.new
end
def show
@user = User.find(params[:id])
end
def create
@user = User.new(user_params)
if @user.save
redirect_to @user
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :marks)
end
end

View file

@ -0,0 +1,14 @@
module SessionsHelper
def log_in(user)
session[:user_id] = user.id
end
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
def logged_in?
!current_user.nil?
end
end

View file

@ -1,3 +1,13 @@
class User < ActiveRecord::Base
validates :name, presence: true, length: { maximum: 50 }
after_initialize :init
validates :name, presence: true, length: { maximum: 50 },
uniqueness: true
def init
self.marks ||= 0
self.role ||= "user"
end
end

View file

@ -5,8 +5,26 @@
<ul class="nav navbar-nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Overview", overview_path %></li>
<li><%= link_to "Help", '#' %></li>
<li><%= link_to "Log in", '#' %></li>
<li><%= link_to "Help", help_path %></li>
<!-- account -->
<% if logged_in? %>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"></li>
<li>
<%= link_to "Log out", logout_path, method: "delete" %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Log in", login_path %></li>
<% end %>
</ul>
</nav>

View file

@ -0,0 +1,19 @@
<h1>Sessions#new</h1>
<p>Find me in app/views/sessions/new.html.erb</p>
<h1>Log in</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(:session, url: login_path) do |f| %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.submit "Log in", class: "btn btn-primary" %>
<% end %>
<p>New user? <%= link_to "Sign up now!", signup_path %></p>
</div>
</div>

View file

@ -0,0 +1 @@
<h1> HELP MIJ!!!! O.O </h1>

View file

@ -1,2 +1,4 @@
<h1>StaticPages#home</h1>
<p>Find me in app/views/static_pages/home.html.erb</p>
<%= link_to "Sign up now!", signup_path , class: "btn btn-lg btn-primary" %>

View file

@ -1,2 +1,17 @@
<h1>Users#new</h1>
<p>Find me in app/views/users/new.html.erb</p>
<h1>Sign up</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@user) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
</br>
<%= f.label :marks %>
<%= f.number_field :marks %> (tijdelijk)
</br>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>

View file

@ -1,14 +1,24 @@
Rails.application.routes.draw do
root 'static_pages#home'
get 'sessions/new'
get 'users/new'
get 'static_pages/order'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'static_pages/home'
get 'overview' => 'static_pages#overview'
get 'static_pages/overview'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
# The priority is based upon order of creation: first created -> highest priority.

View file

@ -0,0 +1,9 @@
require 'test_helper'
class SessionsControllerTest < ActionController::TestCase
test "should get new" do
get :new
assert_response :success
end
end

View file

@ -0,0 +1,4 @@
require 'test_helper'
class SessionsHelperTest < ActionView::TestCase
end