users en sessies
This commit is contained in:
parent
44185e8552
commit
806dc1d8f7
17 changed files with 158 additions and 7 deletions
|
@ -12,5 +12,6 @@
|
||||||
//
|
//
|
||||||
//= require jquery
|
//= require jquery
|
||||||
//= require jquery_ujs
|
//= require jquery_ujs
|
||||||
|
//= require bootstrap
|
||||||
//= require turbolinks
|
//= require turbolinks
|
||||||
//= require_tree .
|
//= require_tree .
|
||||||
|
|
3
app/assets/javascripts/sessions.js.coffee
Normal file
3
app/assets/javascripts/sessions.js.coffee
Normal 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/
|
3
app/assets/stylesheets/sessions.css.scss
Normal file
3
app/assets/stylesheets/sessions.css.scss
Normal 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/
|
|
@ -2,4 +2,5 @@ class ApplicationController < ActionController::Base
|
||||||
# Prevent CSRF attacks by raising an exception.
|
# Prevent CSRF attacks by raising an exception.
|
||||||
# For APIs, you may want to use :null_session instead.
|
# For APIs, you may want to use :null_session instead.
|
||||||
protect_from_forgery with: :exception
|
protect_from_forgery with: :exception
|
||||||
|
include SessionsHelper
|
||||||
end
|
end
|
||||||
|
|
19
app/controllers/sessions_controller.rb
Normal file
19
app/controllers/sessions_controller.rb
Normal 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
|
|
@ -8,4 +8,8 @@ class StaticPagesController < ApplicationController
|
||||||
|
|
||||||
def order
|
def order
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def help
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,26 @@
|
||||||
class UsersController < ApplicationController
|
class UsersController < ApplicationController
|
||||||
def new
|
def new
|
||||||
|
@user = User.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@user = User.find(params[:id])
|
@user = User.find(params[:id])
|
||||||
end
|
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
|
end
|
||||||
|
|
14
app/helpers/sessions_helper.rb
Normal file
14
app/helpers/sessions_helper.rb
Normal 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
|
|
@ -1,3 +1,13 @@
|
||||||
class User < ActiveRecord::Base
|
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
|
end
|
||||||
|
|
|
@ -5,8 +5,26 @@
|
||||||
<ul class="nav navbar-nav pull-right">
|
<ul class="nav navbar-nav pull-right">
|
||||||
<li><%= link_to "Home", root_path %></li>
|
<li><%= link_to "Home", root_path %></li>
|
||||||
<li><%= link_to "Overview", overview_path %></li>
|
<li><%= link_to "Overview", overview_path %></li>
|
||||||
<li><%= link_to "Help", '#' %></li>
|
<li><%= link_to "Help", help_path %></li>
|
||||||
<li><%= link_to "Log in", '#' %></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>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
19
app/views/sessions/new.html.erb
Normal file
19
app/views/sessions/new.html.erb
Normal 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>
|
1
app/views/static_pages/help.html.erb
Normal file
1
app/views/static_pages/help.html.erb
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<h1> HELP MIJ!!!! O.O </h1>
|
|
@ -1,2 +1,4 @@
|
||||||
<h1>StaticPages#home</h1>
|
<h1>StaticPages#home</h1>
|
||||||
<p>Find me in app/views/static_pages/home.html.erb</p>
|
<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" %>
|
|
@ -1,2 +1,17 @@
|
||||||
<h1>Users#new</h1>
|
<h1>Sign up</h1>
|
||||||
<p>Find me in app/views/users/new.html.erb</p>
|
|
||||||
|
<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>
|
|
@ -1,14 +1,24 @@
|
||||||
Rails.application.routes.draw do
|
Rails.application.routes.draw do
|
||||||
|
|
||||||
|
root 'static_pages#home'
|
||||||
|
|
||||||
|
get 'sessions/new'
|
||||||
|
|
||||||
get 'users/new'
|
get 'users/new'
|
||||||
|
|
||||||
get 'static_pages/order'
|
get 'static_pages/order'
|
||||||
|
get 'help' => 'static_pages#help'
|
||||||
root 'static_pages#home'
|
|
||||||
get 'static_pages/home'
|
get 'static_pages/home'
|
||||||
get 'overview' => 'static_pages#overview'
|
get 'overview' => 'static_pages#overview'
|
||||||
get '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
|
resources :users
|
||||||
|
|
||||||
# The priority is based upon order of creation: first created -> highest priority.
|
# The priority is based upon order of creation: first created -> highest priority.
|
||||||
|
|
9
test/controllers/sessions_controller_test.rb
Normal file
9
test/controllers/sessions_controller_test.rb
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class SessionsControllerTest < ActionController::TestCase
|
||||||
|
test "should get new" do
|
||||||
|
get :new
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
4
test/helpers/sessions_helper_test.rb
Normal file
4
test/helpers/sessions_helper_test.rb
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class SessionsHelperTest < ActionView::TestCase
|
||||||
|
end
|
Loading…
Reference in a new issue