diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index d6925fa..e3a9f2d 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -12,5 +12,6 @@ // //= require jquery //= require jquery_ujs +//= require bootstrap //= require turbolinks //= require_tree . diff --git a/app/assets/javascripts/sessions.js.coffee b/app/assets/javascripts/sessions.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/sessions.js.coffee @@ -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/ diff --git a/app/assets/stylesheets/sessions.css.scss b/app/assets/stylesheets/sessions.css.scss new file mode 100644 index 0000000..ccb1ed2 --- /dev/null +++ b/app/assets/stylesheets/sessions.css.scss @@ -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/ diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..d8b3d09 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..60381ac --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -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 diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index c8f0d46..1341d59 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -8,4 +8,8 @@ class StaticPagesController < ApplicationController def order end + + def help + end + end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 8dfd233..a00f0ee 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb new file mode 100644 index 0000000..c4b5118 --- /dev/null +++ b/app/helpers/sessions_helper.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index d95d942..5d09561 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index dfaa099..0f7168d 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -5,8 +5,26 @@
diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb new file mode 100644 index 0000000..095a8e9 --- /dev/null +++ b/app/views/sessions/new.html.erb @@ -0,0 +1,19 @@ +Find me in app/views/sessions/new.html.erb
+New user? <%= link_to "Sign up now!", signup_path %>
+Find me in app/views/static_pages/home.html.erb
+ +<%= link_to "Sign up now!", signup_path , class: "btn btn-lg btn-primary" %> \ No newline at end of file diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index c21a1ad..4167ce0 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -1,2 +1,17 @@ -Find me in app/views/users/new.html.erb
+