User Controllers
This commit is contained in:
parent
9ba59608cb
commit
df80ce9062
17 changed files with 82 additions and 54 deletions
3
app/assets/javascripts/home.js.coffee
Normal file
3
app/assets/javascripts/home.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/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/home.css.scss
Normal file
3
app/assets/stylesheets/home.css.scss
Normal file
|
@ -0,0 +1,3 @@
|
|||
// Place all the styles related to the home controller here.
|
||||
// They will automatically be included in application.css.
|
||||
// You can use Sass (SCSS) here: http://sass-lang.com/
|
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/
|
|
@ -1,5 +1,8 @@
|
|||
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
|
||||
helper_method :current_user
|
||||
|
||||
def current_user
|
||||
@current_user ||= User.find(session[:user_id]) if session[:user_id]
|
||||
end
|
||||
end
|
||||
|
|
4
app/controllers/home_controller.rb
Normal file
4
app/controllers/home_controller.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
class HomeController < ApplicationController
|
||||
def show
|
||||
end
|
||||
end
|
14
app/controllers/sessions_controller.rb
Normal file
14
app/controllers/sessions_controller.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
class SessionsController < ApplicationController
|
||||
def create
|
||||
user = User.from_omniauth(env['omniauth.auth'])
|
||||
session[:user_id] = user.id
|
||||
|
||||
redirect_to root_path
|
||||
end
|
||||
|
||||
def destroy
|
||||
session[:user_id] = nil
|
||||
|
||||
redirect_to root_path
|
||||
end
|
||||
end
|
2
app/helpers/home_helper.rb
Normal file
2
app/helpers/home_helper.rb
Normal file
|
@ -0,0 +1,2 @@
|
|||
module HomeHelper
|
||||
end
|
2
app/helpers/sessions_helper.rb
Normal file
2
app/helpers/sessions_helper.rb
Normal file
|
@ -0,0 +1,2 @@
|
|||
module SessionsHelper
|
||||
end
|
2
app/views/home/show.html.erb
Normal file
2
app/views/home/show.html.erb
Normal file
|
@ -0,0 +1,2 @@
|
|||
<h1>Home#show</h1>
|
||||
<p>Find me in app/views/home/show.html.erb</p>
|
2
app/views/sessions/create.html.erb
Normal file
2
app/views/sessions/create.html.erb
Normal file
|
@ -0,0 +1,2 @@
|
|||
<h1>Sessions#create</h1>
|
||||
<p>Find me in app/views/sessions/create.html.erb</p>
|
2
app/views/sessions/destroy.html.erb
Normal file
2
app/views/sessions/destroy.html.erb
Normal file
|
@ -0,0 +1,2 @@
|
|||
<h1>Sessions#destroy</h1>
|
||||
<p>Find me in app/views/sessions/destroy.html.erb</p>
|
|
@ -1,56 +1,10 @@
|
|||
Rails.application.routes.draw do
|
||||
# The priority is based upon order of creation: first created -> highest priority.
|
||||
# See how all your routes lay out with "rake routes".
|
||||
get 'auth/:provider/callback', to: 'sessions#create'
|
||||
get 'auth/failure', to: redirect('/')
|
||||
get 'logout', to: 'sessions#destroy', as: 'logout'
|
||||
|
||||
# You can have the root of your site routed with "root"
|
||||
# root 'welcome#index'
|
||||
resources :sessions, only: [ :create, :destroy ]
|
||||
resources :home, only: [ :show ]
|
||||
|
||||
# Example of regular route:
|
||||
# get 'products/:id' => 'catalog#view'
|
||||
|
||||
# Example of named route that can be invoked with purchase_url(id: product.id)
|
||||
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
|
||||
|
||||
# Example resource route (maps HTTP verbs to controller actions automatically):
|
||||
# resources :products
|
||||
|
||||
# Example resource route with options:
|
||||
# resources :products do
|
||||
# member do
|
||||
# get 'short'
|
||||
# post 'toggle'
|
||||
# end
|
||||
#
|
||||
# collection do
|
||||
# get 'sold'
|
||||
# end
|
||||
# end
|
||||
|
||||
# Example resource route with sub-resources:
|
||||
# resources :products do
|
||||
# resources :comments, :sales
|
||||
# resource :seller
|
||||
# end
|
||||
|
||||
# Example resource route with more complex sub-resources:
|
||||
# resources :products do
|
||||
# resources :comments
|
||||
# resources :sales do
|
||||
# get 'recent', on: :collection
|
||||
# end
|
||||
# end
|
||||
|
||||
# Example resource route with concerns:
|
||||
# concern :toggleable do
|
||||
# post 'toggle'
|
||||
# end
|
||||
# resources :posts, concerns: :toggleable
|
||||
# resources :photos, concerns: :toggleable
|
||||
|
||||
# Example resource route within a namespace:
|
||||
# namespace :admin do
|
||||
# # Directs /admin/products/* to Admin::ProductsController
|
||||
# # (app/controllers/admin/products_controller.rb)
|
||||
# resources :products
|
||||
# end
|
||||
root to: 'home#show'
|
||||
end
|
||||
|
|
9
test/controllers/home_controller_test.rb
Normal file
9
test/controllers/home_controller_test.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
require 'test_helper'
|
||||
|
||||
class HomeControllerTest < ActionController::TestCase
|
||||
test "should get show" do
|
||||
get :show
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
end
|
14
test/controllers/sessions_controller_test.rb
Normal file
14
test/controllers/sessions_controller_test.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
require 'test_helper'
|
||||
|
||||
class SessionsControllerTest < ActionController::TestCase
|
||||
test "should get create" do
|
||||
get :create
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should get destroy" do
|
||||
get :destroy
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
end
|
4
test/helpers/home_helper_test.rb
Normal file
4
test/helpers/home_helper_test.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
require 'test_helper'
|
||||
|
||||
class HomeHelperTest < ActionView::TestCase
|
||||
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